“表达式必须计算为节点集." [英] "Expression must evaluate to a node-set."

查看:79
本文介绍了“表达式必须计算为节点集."的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有问题

我的XML文件在这里:

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>

我如何解析此XML文件

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

推荐答案

错误原因

根据错误消息, .SelectNodes()要求 xpath 字符串参数求值为节点集,例如此xpath将返回包含三个节点的 XmlNodeList :

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");

提供返回单个节点的 xpath 也是可以接受的-返回的 XmlNodeList 仅具有单个节点:

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.

代替 XmlDocument ,您需要创建一个导航器,编译一个表达式,然后对其求值:

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)

如果您将Xml解析堆栈从使用 XmlDocument 切换为 Linq to Xml XDocument ,则会出现

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)");

格式不正确的Xpath

对于完全无效的 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的问题

您的Xpath错误.您可能想要的是这样:

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

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

将返回 DOVIZ 元素的 ALIS 子元素,该元素的 ADI 子元素的值为 dovizKuru2 (可能是货币变量,例如 USD )

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)

这篇关于“表达式必须计算为节点集."的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆