System.Xml.XPath.XPathException: expression must be evaluated using node-set when executing SelectSingleNode ("// (artist | author)")

Can someone explain to me why this is not working?

I am doing

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

and i get

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

but it works and does not raise an exception even if there are many executing nodes

 XmlNode xmlNode = xmlDocument.SelectSingleNode("//artist"); 
+4
source share
2 answers

As far as I know, you can use '|' just at the top level of the XPath query, so try running the query

  "//artist|//author" 

So far, a recursive search (//) is not very fast, so make sure your dom document is small.

Update:

I looked it up in the spec :

3.3 Node-sets

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

The | The operator computes the union of its operands, which should be Node-sets.

This means that you write left and right of "|" should be used as an xpath request by itself, "|" it just creates an alliance from it.

In particular, you cannot say "search recursively for (something called by the author OR something called by the artist)" because "something called by the author" does not evaluate the result of the xpath request (node ​​set).

+7
source
  • //artist|//author works with XPATH 1.0 and 2.0
  • //(artist|author) works with XPATH 2.0

Microsoft is a lazy corporation. Their infrastructure only supports XPATH 1.0

0
source

All Articles