首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >XPathNodeIterator上时间循环的问题

XPathNodeIterator上时间循环的问题
EN

Stack Overflow用户
提问于 2013-10-10 06:32:22
回答 1查看 120关注 0票数 0

我正在尝试在XPathNodeIterator对象上执行while循环

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

现在,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
复制
XPathNodeIterator xpCategories = GetCategories().Current.Select("/root/category/id");    
while (xpCategories.MoveNext())
Console.WriteLine(xpCategories.Current.Value);

但是这个循环在那之后只工作了一次,我不明白出了什么问题?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-10-10 06:35:15

它应该是

代码语言:javascript
复制
while (xpCategories.MoveNext())
{
    XPathNavigator n = xpCategories.Current;
    Console.WriteLine(n.Value);
}

代码语言:javascript
复制
foreach (XPathNavigator n in xpCategories)Console.WriteLine(n.Value);

虽然我推荐LINQ2XML

代码语言:javascript
复制
XDocument doc=XDocument.Load(xmlPath);
List<string> ids=doc.Elements("category")
                    .Select(x=>x.Element("id").Value)
                    .ToList();
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19288660

复制
相关文章

相似问题

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