Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 循环遍历C中XML的特定节点#_C#_Xml_Xmldocument_Xmlnodelist - Fatal编程技术网

C# 循环遍历C中XML的特定节点#

C# 循环遍历C中XML的特定节点#,c#,xml,xmldocument,xmlnodelist,C#,Xml,Xmldocument,Xmlnodelist,我做了很多研究,但我找不到解决我这个问题的办法。我必须读取C#中的外部xml文件并读取对象中的值。以下是我的xml文件的快照: <DatabaseList> <DatabaseDetails> <ConnectionId>1</ConnectionId> <ConnectionName>MyConn1</ConnectionName> <ServerConnection xmlns:i=&

我做了很多研究,但我找不到解决我这个问题的办法。我必须读取C#中的外部xml文件并读取对象中的值。以下是我的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>

请告知

尝试放入数据表

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

        }
    }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用系统数据;
使用System.Xml;
使用System.Xml.Linq;
命名空间控制台应用程序1
{
班级计划
{
常量字符串文件名=@“c:\temp\test.xml”;
静态void Main(字符串[]参数)
{
XDocument doc=XDocument.Load(文件名);
DataTable dt=新的DataTable();
添加(“ConnectionId”,typeof(string));
添加(“ConnectionName”,typeof(string));
添加(“CobConnection”,typeof(string));
Add(“CobConnectionType”,typeof(string));
添加(“CobDatabase”,typeof(string));
Add(“CobServer”,typeof(string));
添加(“ConnectionType”,typeof(string));
添加(“数据库”,typeof(字符串));
添加(“端口”,类型(字符串));
添加(“服务器”,typeof(字符串));
List details=doc.substands(“DatabaseDetails”).ToList();
foreach(详细信息中的XElement细节)
{
stringid=(string)detail.Element(“ConnectionId”);
字符串名称=(字符串)detail.Element(“ConnectionName”);
XElement xCobConnection=detail.substands(“CobConnection”).FirstOrDefault();
字符串cobConnection=(字符串)xCobConnection.Element(“cobConnection”);
string cobType=(string)xCobConnection.Element(“ConnectionType”);
字符串cobDatabase=(string)xCobConnection.Element(“数据库”);
字符串cobServer=(字符串)xCobConnection.Element(“服务器”);
XElement serverConnection=detail.Element(“serverConnection”);
string connectionType=(string)serverConnection.Element(“connectionType”);
字符串数据库=(字符串)serverConnection.Element(“数据库”);
字符串端口=(字符串)serverConnection.Element(“端口”);
stringserver=(string)serverConnection.Element(“server”);
dt.Rows.Add(新对象[]){
身份证件
名称
cobConnection,
cobType,
COB数据库,
cobServer,
连接类型,
数据库
港口,
服务器
});
}
}
}
}
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
                });
            }

        }
    }
}