C# System.Xml.XPath.XPathException:执行 SelectSingleNode("//(artist|author)") 时,表达式必须计算为节点集

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/631173/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 11:04:19  来源:igfitidea点击:

System.Xml.XPath.XPathException: Expression must evaluate to a node-set when executing SelectSingleNode("//(artist|author)")

c#xmlxpathselectsinglenode

提问by knoopx

Can somebody explain me why is this not working?

有人可以解释我为什么这不起作用吗?

I'm executing

我正在执行

XmlNode xmlNode = xmlDocument.SelectSingleNode("//(artist|author)");

and I get

我得到

System.Xml.XPath.XPathException: Expression must evaluate to a node-set.

but this works and does not raise the exception even when there are many artist nodes

但这有效并且即使有许多艺术家节点也不会引发异常

XmlNode xmlNode = xmlDocument.SelectSingleNode("//artist");

采纳答案by froh42

To my knowledge you can use '|' just at the top level of an XPath Query, so try the query

据我所知,您可以使用“|” 只是在 XPath 查询的顶层,所以尝试查询

    "//artist|//author"

Bye the way doing recursive searches (//) isn't very fast, so make sure your dom document is small.

再见了,递归搜索 (//) 的速度不是很快,因此请确保您的 dom 文档很小。

Update:

更新:

I looked it up in the specification:

我在规范中查了一下:

3.3 Node-sets

A location path can be used as an expression. The expression returns the set of nodes selected by the path.

The | operator computes the union of its operands, which must be node-sets.

3.3 节点集

位置路径可以用作表达式。该表达式返回路径选择的节点集。

的| 运算符计算其操作数的并集,它必须是节点集。

That means whatever you write left and right of "|" needs to be usable as an xpath query on its own, the "|" then just creates the union from it.

这意味着无论你在“|”的左边和右边写什么 需要单独用作 xpath 查询,“|” 然后从它创建联合。

Specifically you can not say "search recursively for (something called author OR something called artist)" because "something called author" does not evaluate to the result of an xpath-query (a node set).

具体来说,您不能说“递归搜索(称为作者的东西或称为艺术家的东西)”,因为“称为作者的东西”不会评估 xpath-query(节点集)的结果。

回答by Ergil

  1. //artist|//authorworks with XPATH 1.0 and 2.0
  2. //(artist|author)works with XPATH 2.0
  1. //artist|//author适用于 XPATH 1.0 和 2.0
  2. //(artist|author)适用于 XPATH 2.0

Microsoft is a lazy corporation. Their framework support only XPATH 1.0

微软是一个懒惰的公司。他们的框架仅支持 XPATH 1.0