简体   繁体   中英

Loop through specific Node of XML in C#

I have researched a lot but I cannot find solution to my particular problem. I have to read an external xml file in C# and read the values in an Object. Here is the snapshot of my xml file:

 <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>

For example, ConnectionName = MyConn2 is passed to the procedure below, code should read values for MyConn2. But in my case, I'm selecting the xmlNodesLvl2 correctly, but it starts from the beginning of the file. I need to read the value of node just found in previous step. For that particular Database.ConnectionName, I need to read the node values for eg, Database, ConnectionType, Server, etc. But I'm starting in next step from the beginning. I have put comment in my code //Problem here.

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;
            }
        }
    }
}

Please advise!

Try putting in 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:\temp\test.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
                });
            }

        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM