C# XpathNavigator无法';无法获取xml的内部文本

C# XpathNavigator无法';无法获取xml的内部文本,c#,xml,xpathnavigator,xpathnodeiterator,C#,Xml,Xpathnavigator,Xpathnodeiterator,这是我的xml <?xml version="1.0" encoding="utf-8" ?> <bookstore> <book genre="autobiography" publicationdate="1981-03-22" ISBN="1-861003-11-0"> <author> <title>The Autobiography of Benjamin Franklin</title>

这是我的xml

<?xml version="1.0" encoding="utf-8" ?> 
<bookstore>
<book genre="autobiography" publicationdate="1981-03-22" ISBN="1-861003-11-0">  
    <author>
    <title>The Autobiography of Benjamin Franklin</title>
        <first-name>Benjamin</first-name>
        <last-name>Franklin</last-name>
    </author>
    <price>8.99</price>
</book>
<book genre="novel" publicationdate="1967-11-17" ISBN="0-201-63361-2">    
    <author>
    <title>The Confidence Man</title>
        <first-name>Herman</first-name>
        <last-name>Melville</last-name>
    </author>
    <price>11.99</price>
</book>
</bookstore>
我得到的两个输出相同

本杰明·弗兰克林的自传8.99


我将有条件,如如果(价格>10),然后得到标题。如何修复此问题可以在xpath表达式中直接使用条件

XPathNodeIterator titleNodes = nav.Select("/bookstore/book[price>10]/author/title");

foreach (XPathNavigator titleNode in titleNodes)
{
    var title = titleNode.Value;
    Console.WriteLine(title);
}

可以在xpath表达式中直接使用条件

XPathNodeIterator titleNodes = nav.Select("/bookstore/book[price>10]/author/title");

foreach (XPathNavigator titleNode in titleNodes)
{
    var title = titleNode.Value;
    Console.WriteLine(title);
}
您在此处调用的方法:

var price = node.Select("price");
返回一个,如中所示,您需要通过旧(c#1.0!)样式对其进行实际迭代:

或者更新的
foreach
样式,其功能相同:

var price = node.Select("price");
foreach (XPathNavigator currentPrice in price)
{
    string currentPriceValue = currentPrice.Value;
    Console.WriteLine(currentPriceValue); // 8.99
}
在上述两个示例中,枚举数的当前值在第一次调用
MoveNext()
后使用。在代码中,在第一次调用
MoveNext()
之前使用。正如在报告中所解释的:

最初,枚举数位于集合中第一个元素之前。在读取Current的值之前,必须调用MoveNext方法将枚举数前进到集合的第一个元素否则,当前未定义。

您看到的奇怪行为是在值未定义时使用
Current
的结果。(我有点希望在这种情况下抛出一个异常,但所有这些类都非常古老——我相信可以追溯到c#1.1——当时的编码标准不那么严格。)

如果您确定只有一个
节点,并且不想遍历多个返回的节点,则可以使用LINQ语法选择单个节点:

var currentPriceValue = node.Select("price").Cast<XPathNavigator>().Select(p => p.Value).SingleOrDefault();
Console.WriteLine(currentPriceValue); // 8.99
最后,考虑切换到加载和查询任意XML。它只是比旧的
XmlDocument
API简单得多。

您在这里调用的方法:

var price = node.Select("price");
返回一个,如中所示,您需要通过旧(c#1.0!)样式对其进行实际迭代:

或者更新的
foreach
样式,其功能相同:

var price = node.Select("price");
foreach (XPathNavigator currentPrice in price)
{
    string currentPriceValue = currentPrice.Value;
    Console.WriteLine(currentPriceValue); // 8.99
}
在上述两个示例中,枚举数的当前值在第一次调用
MoveNext()
后使用。在代码中,在第一次调用
MoveNext()
之前使用。正如在报告中所解释的:

最初,枚举数位于集合中第一个元素之前。在读取Current的值之前,必须调用MoveNext方法将枚举数前进到集合的第一个元素否则,当前未定义。

您看到的奇怪行为是在值未定义时使用
Current
的结果。(我有点希望在这种情况下抛出一个异常,但所有这些类都非常古老——我相信可以追溯到c#1.1——当时的编码标准不那么严格。)

如果您确定只有一个
节点,并且不想遍历多个返回的节点,则可以使用LINQ语法选择单个节点:

var currentPriceValue = node.Select("price").Cast<XPathNavigator>().Select(p => p.Value).SingleOrDefault();
Console.WriteLine(currentPriceValue); // 8.99

最后,考虑切换到加载和查询任意XML。它只是比旧的
XmlDocument
API简单得多。

我想得到这个标题,它的价格是>9。如何添加这个condiiton@GANI-stackoverflow问题的首选格式为。如果你想知道为什么XPathNodeIterator.Current会返回垃圾结果,我的答案告诉你。但是,如果您想通过XPath查询查找价格>9的所有书籍标题,请参阅Alexander Petrov的(或者问另一个问题)。如何添加这个condiiton@GANI-stackoverflow问题的首选格式为。如果你想知道为什么XPathNodeIterator.Current会返回垃圾结果,我的答案告诉你。但是,如果您想通过XPath查询查找所有价格>9的图书,请参阅Alexander Petrov的(或者问另一个问题)。