Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"Expression must evaluate to a node-set."

Tags:

c#

xml

xpath

I have a problem

My XML File is here:

<altinkaynak>

   <DOVIZ>
     <ADI>Tarih</ADI>
     <ALIS>24.07.2013 18:59:45</ALIS>
     <SATIS/>
   </DOVIZ>
   <DOVIZ>
      <ADI>USD</ADI>
      <ALIS>1.9120</ALIS>
      <SATIS>1.9220</SATIS>
   </DOVIZ>
   <DOVIZ>
      <ADI>EUR</ADI>
      <ALIS>2.5280</ALIS>
      <SATIS>2.5430</SATIS>
   </DOVIZ> 
</altinkaynak>

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

like image 341
ofince Avatar asked Jul 24 '13 16:07

ofince


2 Answers

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)

like image 95
StuartLC Avatar answered Nov 07 '22 05:11

StuartLC


I was getting this error because my Xpath expression was invalid. I was using == for equality instead of a single =. My expression was [SomeNode == 'SomeValue'] but should have been [SomeNode = 'SomeValue'].

like image 36
Aaron Jensen Avatar answered Nov 07 '22 06:11

Aaron Jensen