c# - 仅从 XPathNavigator 获取第一个值?

标签 c# .net xml xpath xpathdocument

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/

相关文章:

c# - 单击超链接字段时获取 gridview 的索引

c# - 在运行时在 c# 应用程序中添加用户设置

c# - 使用包含 EF 时可能的 SQL 注入(inject)?

xml - XSLT 1.0 (xsltproc) - 无法解析巨大的 XML

xml - 如何将 Excel OpenXML 共享字符串转换为内联字符串?

c# - Java 中的类型

c# - Predicatebuilder group and or queries with inner outer

c# - 如何在 C# 中不区分大小写地解析 float 和 double (以覆盖 "infinity"等)?

.net - 如果用户再次结帐,我是否应该重新使用失败的 PayPal 付款?

php - 使用 guzzle 6 发送 (POST) xml 的正确方法