ofince ofince - 1 year ago 224
C# Question

"Expression must evaluate to a node-set."

I have a problem

My XML File is here;


<ALIS>24.07.2013 18:59:45</ALIS>

How am I parse this XML file

I coded that way but I got a parse error message;

if (tip == DövizKuruTipi2.Alış)
Line 44: return Decimal.Parse(doc.SelectNodes("//ALTINKAYNAK/DOVIZ/ADI=" + dovizKuru2 + "/ALIS")[0].InnerText.Replace('.', ','));

Expression must evaluate to a node-set

Answer Source

Reason for the Error

As per the error message, .SelectNodes() requires that the xpath string parameter evaluates to a node set, e.g. this xpath will return an XmlNodeList containing 3 nodes:

var nodeSet = document.SelectNodes("/altinkaynak/DOVIZ");

Supplying an xpath which returns a single node is also acceptable - the returned XmlNodeList will just have a single node:

var nodeSet = document.SelectNodes("(/altinkaynak/DOVIZ)[1]");

However, it is not possible to return non-node values, such as scalar expressions:

var nodeSet = document.SelectNodes("count(/altinkaynak/DOVIZ)");

Error: Expression must evaluate to a node-set.

Instead for XmlDocument, you would need to create a navigator, compile an expression, and evaluate it:

 var navigator = document.CreateNavigator();
 var expr = navigator.Compile("count(/altinkaynak/DOVIZ)");
 var count = navigator.Evaluate(expr); // 3 (nodes)

If you switch your Xml parsing stack from using XmlDocument to a Linq to Xml XDocument there is a much more concise way to evaluate scalar expressions:

var count = xele.XPathEvaluate("count(/altinkaynak/DOVIZ)");

Badly formed Xpath

This same error (Expression must evaluate to a node-set) is also frequently returned for xpaths which are invalid altogether

 var nodeSet = document.SelectNodes("{Insert some really badly formed xpath here!}");

Error: Expression must evaluate to a node-set.

OP's Question

You have an error in your Xpath. What you probably want is this:

doc.SelectNodes("//ALTINKAYNAK/DOVIZ[ADI='" + dovizKuru2 + "']/ALIS") // ...

which will return the ALIS child of the DOVIZ element which has an ADI child with a value of dovizKuru2 (which is presumably a variable for currency such as USD)

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download