简体   繁体   English

从XPathNavigator仅获取第一个值?

[英]Get only the first value from XPathNavigator?

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);
}

How can i access straight to the first /bookstore/book/title" child and get his value. 我如何直接访问第一个“ /bookstore/book/title"孩子并获得他的价值。

I dont want to iterate and break the loop 我不想迭代并打破循环

the result should be "The Gorgias3" 结果应该是"The Gorgias3"

ps i dont want to modify the xpath expression with the first filter. ps我不想用第一个过滤器修改xpath表达式。

I want to Know how i can do it with c#. 我想知道如何用C#做到这一点。

Use: 采用:

var node = navigator.SelectSingleNode("/bookstore/book/title");

Console.WriteLine(node);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM