在C#中遍历XML的特定节点



我研究了很多,但找不到解决特定问题的方法。我必须读取C#中的外部xml文件,并读取Object中的值。这是我的xml文件的快照:

<DatabaseList>
<DatabaseDetails>
<ConnectionId>1</ConnectionId>
<ConnectionName>MyConn1</ConnectionName>
<ServerConnection xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<CobConnection>
<CobConnection />
<ConnectionType>MSSQL</ConnectionType>
<Database />
<Server />
</CobConnection>
<ConnectionType>MSSQL</ConnectionType>
<Database>MyDB1</Database>
<Port>2431</Port>
<Server>MyServerName1</Server>
</ServerConnection>
</DatabaseDetails>
<DatabaseDetails>
<ConnectionId>2</ConnectionId>
<ConnectionName>MyConn2</ConnectionName>
<ServerConnection xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<CobConnection>
<CobConnection />
<ConnectionType>MSSQL</ConnectionType>
<Database />
<Server />
</CobConnection>
<ConnectionType>MSSQL</ConnectionType>
<Database>MyDB2</Database>
<Port>2431</Port>
<Server> MyServerName2</Server>
</ServerConnection>
</DatabaseDetails>
</DatabaseList>

例如,ConnectionName=MyConn2被传递到下面的过程,代码应该读取MyConn2的值。但在我的情况下,我正确地选择了xmlNodesLvl2,但它从文件的开头开始。我需要读取上一步中刚刚找到的节点的值。对于特定的数据库。ConnectionName,我需要读取节点值,例如Database、ConnectionType、Server等。但我将从头开始下一步。我已经在我的代码//问题中添加了注释。

public static void GetInfo(string ConnectionName)
{          
XmlDocument xmlDoc = new XmlDocument();
bool bfound = false;
xmlDoc.Load(@"C:path..Database.xml");
XmlNodeList xmlNodesLvl1 = xmlDoc.SelectNodes("DatabaseList/DatabaseDetails");
foreach (XmlNode xmlNode in xmlNodesLvl1)
{
if (xmlNode.HasChildNodes)
{
foreach (XmlNode item in xmlNode.ChildNodes)
{
string tagName = item.Name;
if (tagName == "ConnectionId")
{
Database.ConnectionId = item.InnerText;
}
if (tagName == "ConnectionName")
{
if (item.InnerText == ConnectionName)
{
Database.ConnectionName = item.InnerText;
bfound = true;
XmlNodeList xmlNodesLvl2 = null;
//Problem here
if (Enviroment == "COB")
{
xmlNodesLvl2 = xmlDoc.SelectNodes("DatabaseList/DatabaseDetails/ServerConnection/CobConnection");
}
else
{
xmlNodesLvl2 = xmlDoc.SelectNodes("DatabaseList/DatabaseDetails/ServerConnection");
}                                
foreach (XmlNode xmlNodeLvl2 in xmlNodesLvl2)
{
if (xmlNodeLvl2.HasChildNodes)
{
foreach (XmlNode itemLvl2 in xmlNodeLvl2.ChildNodes)
{
if (itemLvl2.Name == "CobConnection")
{
Database.CobConnection = itemLvl2.InnerText;
}
if (itemLvl2.Name == "Database")
{
Database.Name = itemLvl2.InnerText;
}
if (itemLvl2.Name == "ConnectionType")
{
Database.ConnectionType = itemLvl2.InnerText;
}
if (itemLvl2.Name == "Server")
{
Database.Server = itemLvl2.InnerText;
}
}
if (bfound == true)
{
break;
}
}
}
if (bfound == true)
{
break;
}
}
}
}
if (bfound == true)
{
break;
}
}
}
}

请告知!

尝试放入DataTable

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Xml;
using System.Xml.Linq;
namespace ConsoleApplication1
{
class Program
{
const string FILENAME = @"c:temptest.xml";
static void Main(string[] args)
{
XDocument doc = XDocument.Load(FILENAME);
DataTable dt = new DataTable();
dt.Columns.Add("ConnectionId",typeof(string));
dt.Columns.Add("ConnectionName",typeof(string));
dt.Columns.Add("CobConnection",typeof(string));
dt.Columns.Add("CobConnectionType",typeof(string));
dt.Columns.Add("CobDatabase",typeof(string));
dt.Columns.Add("CobServer",typeof(string));
dt.Columns.Add("ConnectionType",typeof(string));
dt.Columns.Add("Database",typeof(string));
dt.Columns.Add("Port",typeof(string));
dt.Columns.Add("Server", typeof(string));
List<XElement> details = doc.Descendants("DatabaseDetails").ToList();
foreach (XElement detail in details)
{
string id = (string)detail.Element("ConnectionId");
string name = (string)detail.Element("ConnectionName");
XElement xCobConnection = detail.Descendants("CobConnection").FirstOrDefault();
string cobConnection = (string)xCobConnection.Element("CobConnection");
string cobType = (string)xCobConnection.Element("ConnectionType");
string cobDatabase = (string)xCobConnection.Element("Database");
string cobServer = (string)xCobConnection.Element("Server");

XElement serverConnection = detail.Element("ServerConnection");
string connectionType = (string)serverConnection.Element("ConnectionType");
string database = (string)serverConnection.Element("Database");
string port = (string)serverConnection.Element("Port");
string server = (string)serverConnection.Element("Server");
dt.Rows.Add(new object[] {
id,
name,
cobConnection,
cobType,
cobDatabase,
cobServer,
connectionType,
database,
port,
server
});
}
}
}
}

相关内容

  • 没有找到相关文章

最新更新