有人能解释一下为什么这不起作用吗?
我正在执行
XmlNode xmlNode = xmlDocument.SelectSingleNode("//(artist|author)");
我明白了
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");
最佳答案
据我所知,您可以使用“|”就在 XPath 查询的顶层,所以尝试查询
"//artist|//author"
再见,递归搜索 (//) 的速度不是很快,因此请确保您的 dom 文档很小。
更新:
我在 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.
这意味着无论你在“|”的左边和右边写什么需要单独用作 xpath 查询,“|”然后从中创建联合。
具体来说,您不能说“递归搜索(称为作者的东西或称为艺术家的东西)”,因为“称为作者的东西”不会评估 xpath 查询(节点集)的结果。
关于c# - 系统.Xml.XPath.XPathException : Expression must evaluate to a node-set when executing SelectSingleNode ("//(artist|author)"),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/631173/