views:

2800

answers:

2

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");
+4  A: 

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

    "//artist|//author"

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

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.

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.

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).

froh42
A: 
  1. //artist|//author works with XPATH 1.0 and 2.0
  2. //(artist|author) works with XPATH 2.0

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

Ergil