简体   繁体   English

XElement.XPathEvaluate 未正确评估指数数

[英]XElement.XPathEvaluate not evaluating exponential numbers correctly

In my C# Program, I am loading a System.Xml.Linq.Xelement variable with a portion of an XML file I'm trying to parse and feeding it various XPaths to evaluate values for using the XPathEvaluate() method.在我的 C# 程序中,我正在加载一个System.Xml.Linq.Xelement变量和 XML 文件的一部分,我试图解析并向它提供各种 XPath 以评估使用XPathEvaluate()方法的值。

My challenge is that my XML has some numbers stored as exponential numbers and the XPath is ignoring them even though I'm reading that XPath 2.0 should allow for these being understood to be numbers.我的挑战是我的 XML 有一些数字存储为指数数字,而 XPath 忽略它们,即使我读到 XPath 2.0 应该允许将这些理解为数字。

A sample would look as follows:示例如下所示:

XML ( childNode ): XML(子childNode ):

<Parent>
<Vals id="Val1">
  <abc>1.8</abc>
</Vals>
<Vals id="Val2">
  <abc>4.8552829108959736E-5</abc>
  <def>4.940657864520423E-4</def>
  <ghi>0.1262403356384331</ghi>
  <jkl>0.0</jkl>
</Vals>
</Parent>

XPath ( myXPath ): XPath ( myXPath ):

sum(Parent/Vals[@id="Val2"]/*[self::abc | self::def | self::ghi | self::jkl][number(.)=number(.)])

and my code looks like:我的代码看起来像:

var value= childNode.XPathEvaluate(myXPath);

I would expect the value to be:我希望价值是:

value = 4.8552829108959736E-5 + 4.940657864520423E-4 + 0.126240335638433 + 0
      = 0.126782954253994

But, rather I get:但是,我得到:

value = 0.126240335638433 + 0
      = 0.126240335638433

Any thoughts about any way this can be fixed in my C# code?关于可以在我的 C# 代码中修复的任何方法的任何想法?

XPath 2.0 does allow them -- but XPathEvaluate only supports XPath 1.0. XPath 2.0 确实允许它们——但XPathEvaluate仅支持 XPath 1.0。 This is not a shortcoming of XElement ;这不是XElement的缺点; .NET never added support past 1.0. .NET 从未在 1.0 之后添加支持。

If you must use XPath here, see this list for third-party support.如果您必须在此处使用 XPath,请参阅此列表以获取第三方支持。

If you don't need to do use XPath for the calculation per se but are just looking for a way to do this in C#, that's simple enough of course:如果您不需要使用 XPath 进行计算,而只是在 C# 中寻找一种方法来执行此操作,那当然很简单:

var value = x.XPathSelectElements(@"/Vals[@id=""Val2""]/*").Sum(s => (double) s);

But if the Sum part is supposed to be performed dynamically from arbitrary expressions you can pass in, you'll need a full XPath processor.但是如果Sum部分应该从您可以传入的任意表达式动态执行,您将需要一个完整的 XPath 处理器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM