0

此代码打印出来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);
4

1 回答 1

0

您需要使用例如 XPathNodeIterator

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

或者在访问之前至少调用NodeIter.MoveNext()一次Current.Value

于 2013-11-08T20:52:58.067 回答