Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

getting strongly typed results from an XPath query?

1 view
Skip to first unread message

David Thielen

unread,
Nov 12, 2009, 1:22:17 PM11/12/09
to
Hi;

Is there an easy way, when I am iterating through the nodes returned
from an XPath query, to get typed results. I need to know if it's a
DateTime, decimal, or string. This is more than just what node it is
because an XPath can be the result of a count or other operation where
the resulting type has nothing to do with any node type.

I optionally have a schema - but not always.

??? - thanks - dave


david@at-at-at@windward.dot.dot.net
Windward Reports -- http://www.WindwardReports.com
me -- http://dave.thielen.com

Cubicle Wars - http://www.windwardreports.com/film.htm

Colbert Zhou [MSFT]

unread,
Nov 13, 2009, 5:12:58 AM11/13/09
to
Hello Dave,

We can call XPathNavigator.Evaluate method to evaluate a XPathExpression,
and returns the typed result. The following example evaluates an
XPathExpression and returns a Double using the Current node of the
XPathNodeIterator as the context node,

----------------------------------------------------------------------------
------------------------
XPathDocument document = new XPathDocument("books.xml");
XPathNavigator navigator = document.CreateNavigator();

XPathNodeIterator nodes = navigator.Select("//book");
XPathExpression query = nodes.Current.Compile("sum(descendant::price)");

Double total = (double)navigator.Evaluate(query, nodes);
Console.WriteLine("Total price for all books: {0}", total.ToString());
----------------------------------------------------------------------------
-------------------------


Best regards,
Ji Zhou
Microsoft Online Community Support

Martin Honnen

unread,
Nov 13, 2009, 6:11:53 AM11/13/09
to
David Thielen wrote:

> Is there an easy way, when I am iterating through the nodes returned
> from an XPath query, to get typed results. I need to know if it's a
> DateTime, decimal, or string. This is more than just what node it is
> because an XPath can be the result of a count or other operation where
> the resulting type has nothing to do with any node type.

XPath 1.0 only knows the types number (which is a double), string,
boolean, node-set. So I am not sure why you expect a DateTime or decimal
from an XPath expression, unless you use a third party XPath 2.0
implementation where that is possible.

Also if you are "iterating through nodes" then it can't be a count or
other expression not returning a node-set.

Generally, with Microsoft's XPath 1.0 implementation, XPathNavigator, if
you use the Evaluate method
http://msdn.microsoft.com/en-us/library/system.xml.xpath.xpathnavigator.evaluate.aspx
you get a .NET object returned, the mapping of XPath types to .NET types
is as follows:
"The result of the expression (Boolean, number, string, or node set).
This maps to Boolean, Double, String, or XPathNodeIterator objects
respectively."

If you have an XPathNodeIterator and iterate over it then you are
dealing with an XPathNavigator for the current item and XPathNavigator
has properties
(http://msdn.microsoft.com/en-us/library/system.xml.xpath.xpathnavigator_properties.aspx)
TypedValue and ValueType and additionally properties like ValueAsDateTime.


--

Martin Honnen --- MVP XML
http://msmvps.com/blogs/martin_honnen/

David Thielen

unread,
Nov 13, 2009, 1:38:16 PM11/13/09
to
On Fri, 13 Nov 2009 12:11:53 +0100, Martin Honnen <maho...@yahoo.de>
wrote:

Please correct me if I'm worng - this means for a DateTime I will get
a string and need to know the format it is in.

Also, how does it know a result is a number if a node value is
<node>123.45</node> when the next one could be <node>dave</node>?

Martin Honnen

unread,
Nov 13, 2009, 1:51:34 PM11/13/09
to
David Thielen wrote:

>> If you have an XPathNodeIterator and iterate over it then you are
>> dealing with an XPathNavigator for the current item and XPathNavigator
>> has properties
>> (http://msdn.microsoft.com/en-us/library/system.xml.xpath.xpathnavigator_properties.aspx)
>> TypedValue and ValueType and additionally properties like ValueAsDateTime.
>
> Please correct me if I'm worng - this means for a DateTime I will get
> a string and need to know the format it is in.

Has the XML document you are querying been validated successfully? Is
the element type xsd:dateTime?

> Also, how does it know a result is a number if a node value is
> <node>123.45</node> when the next one could be <node>dave</node>?

If there is no schema respectively if the XML document has not been
validated then the no type information is available. You can then try
one of the ValueAsSomeType methods to check whether the untyped string
in the element can be converted to the type, they will throw an
exception if conversion is not possible.

If the XML has been validated then you can't have two elements of the
same name (e.g. 'node') with different types in the same context.

David Thielen

unread,
Nov 16, 2009, 12:59:46 PM11/16/09
to
On Fri, 13 Nov 2009 19:51:34 +0100, Martin Honnen <maho...@yahoo.de>
wrote:

>David Thielen wrote:


>
>>> If you have an XPathNodeIterator and iterate over it then you are
>>> dealing with an XPathNavigator for the current item and XPathNavigator
>>> has properties
>>> (http://msdn.microsoft.com/en-us/library/system.xml.xpath.xpathnavigator_properties.aspx)
>>> TypedValue and ValueType and additionally properties like ValueAsDateTime.
>>
>> Please correct me if I'm worng - this means for a DateTime I will get
>> a string and need to know the format it is in.
>
>Has the XML document you are querying been validated successfully? Is
>the element type xsd:dateTime?

Generally we have to handle XML with no schema. So we'll have to do
the tryAs.

0 new messages