【发布时间】:2016-01-14 23:58:57
【问题描述】:
我有一个需要解析的 XML 字符串。
代码如下:
inputXML = "<elementList xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">
<NodePrime>
<Node>
<NodeA>1</NodeA>
<NodeB>2</NodeB>
</Node>
<Node>
<NodeA>3</NodeA>
<NodeB>4</NodeB>
</Node>
</NodePrime>
</elementList>";
var ItemList = new List<ItemList>();
using(XmlReader Reader = XmlReader.Create(new StringReader(inputXML)))
{
Reader.read();
var doc = XDocument.Load(Reader);
var xmlnode = doc.Descendants("Node");
foreach(var item in xmlHeirarchy)
{
var ListA = new ItemList
{
itemA = item.Element("NodeA").Value;
itemB = item.Element("NodeB").Value;
};
ItemList.Add(ListA );
}
}
我的问题在于 doc.Descendants,因为我的 xmlnode 返回空。
请让我知道我做错了什么以及最好的解决方法。
【问题讨论】:
-
xmlHeirarchy -> xmlnode.这是什么
Reader.read();? -
除了一些拼写错误之外,代码运行良好。你确定这是你的真实代码吗?
inputXML是硬编码的吗? -
尝试类似 doc.Root.Element("NodePrime").Elements().Descendants("Node")
标签: c# xml xml-parsing xmlreader