XElement.XPathEvaluate无法正确解析指数形式数值的问题
Ah, I get it—you’re hitting a common gotcha here. Even though XPath 2.0 does support exponential notation (like 1.2e-4), the default XPath engine used by XElement.XPathEvaluate() in .NET is XPath 1.0, which doesn’t recognize those values as numbers. That’s why your expressions are failing when dealing with exponential-formatted numbers from your XML.
Here are a couple of practical solutions to work around this:
Solution 1: Parse the Exponential Value in C# First
Instead of relying on XPath to parse the exponential string, extract the value as a string in C#, convert it to a numeric type (like double or decimal), then use that parsed value in your XPath expression. This keeps you within the XPath 1.0 constraints without adding external libraries.
Example code:
using System.Globalization; using System.Xml.Linq; // Assume your XML fragment is loaded into an XElement called childNode var valueElement = childNode.Element("YourValueElement"); if (valueElement != null) { // Extract the exponential string and parse it to a double string expValueStr = valueElement.Value; double parsedValue = double.Parse(expValueStr, CultureInfo.InvariantCulture); // Use the parsed value in your XPath (convert back to a non-exponential string for XPath 1.0) string myXPath = $"//YourValueElement/text() = {parsedValue.ToString(CultureInfo.InvariantCulture)}"; var result = childNode.XPathEvaluate(myXPath); }
This works because XPath 1.0 will correctly parse the non-exponential numeric string generated by ToString(CultureInfo.InvariantCulture).
Solution 2: Switch to an XPath 2.0 Engine
If you need to use XPath 2.0 natively (for more complex expressions that rely on exponential notation support), you can use an open-source library like Saxon-HE, which implements XPath 2.0 (and higher).
First, install the Saxon-HE NuGet package, then use this approach:
using Saxon.Api; using System.Xml.Linq; // Load your XML fragment Processor saxonProcessor = new Processor(); DocumentBuilder docBuilder = saxonProcessor.NewDocumentBuilder(); XdmNode saxonDoc = docBuilder.Build(new StringReader(childNode.ToString())); // Configure the XPath compiler to use XPath 2.0 XPathCompiler xPathCompiler = saxonProcessor.NewXPathCompiler(); xPathCompiler.LanguageVersion = XPathVersion.XPath20; // Now your XPath can directly handle exponential notation string myXPath = "//YourValueElement = 1.2e-5"; // Example XPath using exponential value XdmValue result = xPathCompiler.Evaluate(myXPath, saxonDoc);
This lets you write XPath expressions that natively recognize exponential numbers, just like you expected.
Bonus: Custom XPath Extension Function (Advanced)
If you really want to handle the conversion directly in XPath 1.0, you can create a custom extension function that parses exponential strings to numbers. However, this requires more setup (registering the extension with the XPath navigator) and is generally more complex than the above solutions, so it’s only recommended if you have very specific needs.
内容的提问来源于stack exchange,提问作者John Bustos

