C# “表达式必须评估为节点集。”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17839989/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
"Expression must evaluate to a node-set."
提问by ofince
I have a problem
我有个问题
My XML File is here:
我的 XML 文件在这里:
<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
我如何解析这个 XML 文件
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
表达式必须计算为节点集
采纳答案by StuartLC
Reason for the Error
错误原因
As per the error message, .SelectNodes()requires that the xpathstring parameter evaluates to a node set, e.g. this xpath will return an XmlNodeListcontaining 3 nodes:
根据错误消息,.SelectNodes()要求xpath字符串参数计算为节点集,例如此 xpath 将返回一个XmlNodeList包含 3 个节点:
var nodeSet = document.SelectNodes("/altinkaynak/DOVIZ");
Supplying an xpathwhich returns a single node is also acceptable - the returned XmlNodeListwill just have a single node:
提供xpath返回单个节点的an也是可以接受的 - 返回的XmlNodeList将只有一个节点:
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:
而对于XmlDocument,您需要创建一个导航器、编译一个表达式并对其求值:
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 XmlDocumentto a Linq to XmlXDocumentthere is a much more concise wayto evaluate scalar expressions:
如果您将 Xml 解析堆栈从 using 切换XmlDocument到 a Linq to XmlXDocument,则有一种更简洁的方法来评估标量表达式:
var count = xele.XPathEvaluate("count(/altinkaynak/DOVIZ)");
Badly formed Xpath
格式错误的 Xpath
This same error (Expression must evaluate to a node-set) is also frequently returned for xpaths which are invalid altogether
同样的错误 ( Expression must evaluate to a node-set) 也经常为xpath完全无效的 s返回
var nodeSet = document.SelectNodes("{Insert some really badly formed xpath here!}");
Error: Expression must evaluate to a node-set.
错误:表达式必须计算为节点集。
OP's Question
OP的问题
You have an error in your Xpath. What you probably want is this:
您的 Xpath 中有错误。你可能想要的是:
doc.SelectNodes("//ALTINKAYNAK/DOVIZ[ADI='" + dovizKuru2 + "']/ALIS") // ...
which will return the ALISchild of the DOVIZelement which has an ADIchild with a value of dovizKuru2(which is presumably a variable for currency such as USD)
这将返回具有值为的子元素的元素的ALIS子DOVIZ元素(这可能是货币的变量,例如)ADIdovizKuru2USD

