Groups | Blog | Home
all groups > dotnet xml > november 2006 >

dotnet xml : XPath, sum and number conversion



Oleg Tkachenko [MVP]
11/29/2006 12:00:00 AM
[quoted text, click to view]

The result is double number 42.2. 42.19999999996 is memory
representation of floating point number 42.2.
If you are interested take a look at "What Every Computer Scientist
Should Know About Floating-Point Arithmetic" at
http://docs.sun.com/source/806-3568/ncg_goldberg.html.
You probably looking into result in debugger? Try to output it instead.

[quoted text, click to view]

1. You don't need number() function - sum() function returns number.
2. No exception,
sum = navigator.Evaluate("number(sum(//a))")
works for me, but there is no need to use number() function here.


--
Oleg Tkachenko [XML MVP, MCPD]
PAF
11/29/2006 12:04:02 AM
Hi,

I'm trying to evaluate a sum expression trougth .NET 2
System.Xml.XPath.XPathNavigator object.

My code is :

Dim document As New System.Xml.XmlDocument()
document.LoadXml("<?xml
version=""1.0""?><root><a>28.7</a><a>9.18</a><a>4.32</a></root>")
Dim navigator As System.Xml.XPath.XPathNavigator =
document.CreateNavigator()
navigator.Evaluate("sum(//a)")

My XML source is :

<?xml version="1.0"?>
<root>
<a>28.7</a>
<a>9.18</a>
<a>4.32</a>
</root>

When I evaluate this expression, the result is 42.19999999996, and
should be 42.2 ... So I read some news and the W3C recommendation about
conversion to numeric values in
http://www.w3.org/TR/xpath#section-Number-Functions .

There is a function called number() that effectively convert XML string
values to numeric values. But with .NET, it raise a
System.Xml.XPath.XPathException ...

Does MS-XML implements this number function ?? Any idee ?

Thanks a lot.

Best regards, Pascal
AddThis Social Bookmark Button