gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 21:09:17 26 4
gpt4 key购买 nike

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" child 并获得他的值(value)。

我不想迭代和打破循环

结果应该是 "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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com