我正在尝试使用XPathNodeIterator对象
XPathNodeIterator xpCategories = GetCategories();
现在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>
我需要在循环中获取每个类别节点“id”。这样的事情
foreach (char str in xpCategories.Current.Value)
{
xpCategories.MoveNext();
}
但没有用。任何人都可以引导我走向正确的道路吗?
答案 0 :(得分:1)
试试这个:
//Replace with your method to return the XML
XPathDocument document = new XPathDocument("DemoXML.xml");
//*****
XPathNavigator navigator = document.CreateNavigator();
XPathNodeIterator nodes = navigator.Select("/root/category/id");
while (nodes.MoveNext())
Console.WriteLine(nodes.Current.Value);
编辑:
XPathNodeIterator xpCategories = GetCategories();
XPathNodeIterator xpCategoriesID = xpCategories.Current.Select("root/category/id");
while (xpCategoriesID.MoveNext())
Console.WriteLine(xpCategoriesID.Current.Value);
答案 1 :(得分:1)
XPathNodeIterator xpCategories = GetCategories().Current.Select("/root/category/id");
while (xpCategories.MoveNext())
Console.WriteLine(xpCategories.Current.Value);