var xml= @"<?xml version='1.0'?>
<bookstore>
<book genre=""3"">
<title>The Gorgias3</title>
<author>
<name>Plato3</name>
</author>
<price>3</price>
</book>
<book genre=""4"">
<title>The Gorgias4</title>
<author>
<name>Plato4</name>
</author>
<price>4</price>
</book>
</bookstore>";
XPathDocument docNav = new XPathDocument(new StringReader(xml));
XPathNavigator navigator = docNav.CreateNavigator();
XPathNodeIterator NodeIter = navigator.Select("/bookstore/book/title");
foreach (XPathNavigator selectedNode in NodeIter)
{
Console.WriteLine(selectedNode.Name);
}
我如何直接访问第一个 /bookstore/book/title"
子项并获取他的值。
我不想迭代并打破循环
结果应该是“The Gorgias3”
p.s. i dont want to modify the xpath expression with the first filter.
I want to Know how i can do it with c#.
最佳答案
用途:
var node = navigator.SelectSingleNode("/bookstore/book/title");
Console.WriteLine(node);
关于c# - 仅从 XPathNavigator 获取第一个值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9098498/