无法通过XPathNodeIterator使谓词正常工作

时间:2013-11-08 20:02:38

标签: c# xpath

此代码打印出book1book2。我只是想打印出book2。我没有正确指定谓词吗?

(注意我并没有真正寻找另一种打印方式" book2"因为这是我的问题的简化 - 我更希望了解我做错了什么因为我正在学习c#)。

using System.Xml;
using System.Xml.XPath;
using System.IO;

String xpathStg =
    "<bookstore> <book>book1</book> <book>book2</book> </bookstore>";

XPathDocument docNav = new XPathDocument(new StringReader(xpathStg));

XPathNavigator nav = docNav.CreateNavigator();

XPathNodeIterator NodeIter = nav.Select("/bookstore/book[last()]");

Console.WriteLine("NodeIter.Current.Value=" + NodeIter.Current.Value);

1 个答案:

答案 0 :(得分:0)

您需要使用XPathNodeIterator,例如

foreach (XPathNavigator book in NodeIter) { Console.WriteLine(book.Value); }

或至少在访问NodeIter.MoveNext()之前致电Current.Value