首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >XPathNodeIterator上的Foreach循环不能正常工作

XPathNodeIterator上的Foreach循环不能正常工作
EN

Stack Overflow用户
提问于 2013-08-02 07:18:30
回答 2查看 430关注 0票数 0

我正试图预测一个XPathNodeIterator对象

代码语言:javascript
复制
    XPathNodeIterator xpCategories = GetCategories();

现在,xpCategories持有这样一个xml

代码语言:javascript
复制
 <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“,如下所示

代码语言:javascript
复制
foreach (char str in xpCategories.Current.Value)
    {
        xpCategories.MoveNext();
    }

但是没有一个use..Can能指引我走正确的道路吗?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-08-02 08:12:33

试试这个:

代码语言:javascript
复制
  //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);

编辑:

代码语言:javascript
复制
XPathNodeIterator xpCategories = GetCategories();
XPathNodeIterator xpCategoriesID = xpCategories.Current.Select("root/category/id");

while (xpCategoriesID.MoveNext())
    Console.WriteLine(xpCategoriesID.Current.Value);
票数 1
EN

Stack Overflow用户

发布于 2013-08-02 08:54:22

代码语言:javascript
复制
XPathNodeIterator xpCategories = GetCategories().Current.Select("/root/category/id");    
while (xpCategories.MoveNext())
    Console.WriteLine(xpCategories.Current.Value);
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/18010984

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档