我有XPathNodeIterator
名为 _xpCategories ,其中包含此类数据
<root>
<category numberofproducts="0">
<id>hoved</id>
<url>/products/hovednavigation</url>
<name>HOVEDNAVIGATION</name>
<category numberofproducts="0">
<id>embalfors</id>
<url>/products/emballage-og-forsendelse</url>
<name>Emballage og forsendelse</name>
<category numberofproducts="0">
<id>gaveindpak</id>
<url>/products/gaveindpakning</url>
<name>Gaveindpakning</name>
<category numberofproducts="3">
<id>cellofan</id>
<url>/products/cellofan</url>
<name>Cellofan</name>
</category>
<category numberofproducts="30">
<id>gavebånd</id>
<url>/products/gavebaand</url>
<name>Gavebånd</name>
</category>
</category>
<category numberofproducts="0">
<id>kuvkonv</id>
<url>/products/kuverter-og-konvolutter</url>
<name>Kuverter og konvolutter</name>
</category>
</category>
</category>
</root>
我想遍历此 xml 中的每个类别,并打印每个类别的 id 。 我做过这样的代码。
while (_xpCategories.MoveNext())
{
Console.WriteLine(_xpCategories.Current.SelectSingleNode("id"));
}
但是这个循环只能工作一个,只有第一个类别的id得到,然后它存在循环。任何人都可以指出我做错了吗?
答案 0 :(得分:1)
您可以像这样使用XPathNavigator和XPathNodeIterator
(修改后的例子from here)
XPathDocument document = new XPathDocument("yourxml.xml");
XPathNavigator navigator = document.CreateNavigator();
XPathNodeIterator _xpCategories = navigator.Select("//category");
_xpCategories.MoveNext();
while (nodesText.MoveNext())
Console.WriteLine(nodesText.Current.Value);
答案 1 :(得分:1)