我正在尝试在XPathNodeIterator对象上执行while循环
XPathNodeIterator xpCategories = GetCategories().Current.Select("/root/category/id"); 现在,xpCategories持有这样一个xml
<root>
<category numberofproducts="0">
<id>format</id>
<name>Kopi/Print</name>
</category>
<category numberofproducts="1">
<id>frankering</id>
<name>Kopi/Print</name>
</category>
<category numberofproducts="0">
<id>gardbøjler</id>
<name>Møbler</name>
</category>
<category numberofproducts="0">
<id>gardknager</id>
<name>Møbler</name>
</category>
<category numberofproducts="0">
<id>gardspejle</id>
<name>Møbler</name>
</category>
</root>我需要在loop.tried中获取每个类别节点"id“,如下所示
XPathNodeIterator xpCategories = GetCategories().Current.Select("/root/category/id");
while (xpCategories.MoveNext())
Console.WriteLine(xpCategories.Current.Value);但是这个循环在那之后只工作了一次,我不明白出了什么问题?
发布于 2013-10-10 06:35:15
它应该是
while (xpCategories.MoveNext())
{
XPathNavigator n = xpCategories.Current;
Console.WriteLine(n.Value);
}或
foreach (XPathNavigator n in xpCategories)Console.WriteLine(n.Value);虽然我推荐LINQ2XML
XDocument doc=XDocument.Load(xmlPath);
List<string> ids=doc.Elements("category")
.Select(x=>x.Element("id").Value)
.ToList();https://stackoverflow.com/questions/19288660
复制相似问题