0

我正在尝试对 XPathNodeIterator 对象进行 foreach

    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();
    }

但没用..任何人都可以引导我走向正确的道路吗?

4

2 回答 2

1
XPathNodeIterator xpCategories = GetCategories().Current.Select("/root/category/id");    
while (xpCategories.MoveNext())
    Console.WriteLine(xpCategories.Current.Value);
于 2013-08-02T08:54:22.920 回答
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);
于 2013-08-02T08:12:33.260 回答