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 can I parse this XML file?
I coded it this way but I get 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
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 xpath
s 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 - the search value needs to be enclosed in quotes. 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
)