I'm trying to write a code to convert a xml file into a datatable. I'm getting this below error
我正在尝试编写一个代码来将xml文件转换为数据表。我收到以下错误
System.Xml.Linq.XDocument' does not contain a definition for 'DocumentNode' and no extension method 'DocumentNode' accepting a first argument of type 'System.Xml.Linq.XDocument' could be found (are you missing a using directive or an assembly reference?)
System.Xml.Linq.XDocument'不包含'DocumentNode'的定义,也没有可以找到接受类型'System.Xml.Linq.XDocument'的第一个参数的扩展方法'DocumentNode'(你是否缺少using指令或装配参考?)
What am I missing?
我错过了什么?
Below is my code
以下是我的代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml.XPath;
using System.Xml.Linq;
namespace ConsoleApplication1
{
class Program
{
static void Main()
{
var xdoc = System.Xml.Linq.XDocument.Load(filename);
System.Xml.XmlElement xelRoot = xdoc.DocumentElement;
System.Xml.XmlNodeList xList = xelRoot.SelectNodes("/rulebase/security/rules/entry");
int x = 0;
int y = 0;
string[] ColumnArray = new string[25];
string[] RowArray = new string[25];
foreach (System.Xml.XmlNode pNode in xList)
{
if (pNode.ChildNodes.Count = 0)
{
}
else
{
string val1 = "";
string nodeVal = "";
string nodeName = pNode.name;
foreach (System.Xml.XmlNode childNode in pNode.ChildNodes)
{
val1 = val1 + childNode.value + ", ";
}
val1 = val1 + " ,";
nodeVal = val1.substring(0, val1.length - 3);
ColumnArray[x] = nodeName;
RowArray[y] = nodeVal;
x++;
y++;
}
}
System.Data.DataTable table1 = new System.Data.DataTable("entry");
foreach (string s in ColumnArray)
{
table1.Columns.Add(s);
}
System.Data.DataRow row;
row = table1.LoadDataRow(RowArray, true);
}
}
}
1 个解决方案
#1
0
(Initially I didn't understand the comment under the question, so here some details)
(最初我不理解问题下的评论,所以这里有一些细节)
The older System.Xml.XmlDocument has DocumentElement
(this is being used in the code ... I don't know where the DocumentNode mentioned in the question comes from).
较旧的System.Xml.XmlDocument具有DocumentElement(这在代码中使用...我不知道问题中提到的DocumentNode来自哪里)。
- is the newer LINQ to XML class
- which does not have
DocumentElement
. - e.g. use
xdoc.Root.Name
to get the name of the root element
是较新的LINQ to XML类
它没有DocumentElement。
例如使用xdoc.Root.Name获取根元素的名称
For more info XMlDocument vs XDocument
有关更多信息XMlDocument vs XDocument
#1
0
(Initially I didn't understand the comment under the question, so here some details)
(最初我不理解问题下的评论,所以这里有一些细节)
The older System.Xml.XmlDocument has DocumentElement
(this is being used in the code ... I don't know where the DocumentNode mentioned in the question comes from).
较旧的System.Xml.XmlDocument具有DocumentElement(这在代码中使用...我不知道问题中提到的DocumentNode来自哪里)。
- is the newer LINQ to XML class
- which does not have
DocumentElement
. - e.g. use
xdoc.Root.Name
to get the name of the root element
是较新的LINQ to XML类
它没有DocumentElement。
例如使用xdoc.Root.Name获取根元素的名称
For more info XMlDocument vs XDocument
有关更多信息XMlDocument vs XDocument