getting strongly typed results from an XPath query?

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
0
thielen (152)
11/12/2009 6:22:17 PM
dotnet.xml 7266 articles. 0 followers. Follow

5 Replies
1719 Views

Similar Articles

[PageSpeed] 35

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

0
colbertz (6)
11/13/2009 10:12:58 AM
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/
0
mahotrash (1777)
11/13/2009 11:11:53 AM
On Fri, 13 Nov 2009 12:11:53 +0100, Martin Honnen <mahotrash@yahoo.de>
wrote:

>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.

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>?

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
0
thielen (152)
11/13/2009 6:38:16 PM
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.

-- 

	Martin Honnen --- MVP XML
	http://msmvps.com/blogs/martin_honnen/
0
mahotrash (1777)
11/13/2009 6:51:34 PM
On Fri, 13 Nov 2009 19:51:34 +0100, Martin Honnen <mahotrash@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.

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
0
thielen (152)
11/16/2009 5:59:46 PM
Reply:

Similar Artilces:

Product Key Query #2
I have a mac office 2004 product with 3 product keys. What has happened is that i installed it, it read the product key and then nothing happened... so I wasted a whole product key. Can i get help so I can re-use it and also help with the installation. I have a macbook - white - 2.00GHz On 30/1/07 07:28, in article 1170142138.929014.146590@a34g2000cwb.googlegroups.com, "airplanes.simmons@gmail.com" <airplanes.simmons@gmail.com> wrote: > I have a mac office 2004 product with 3 product keys. What has > happened is that i installed it, it read the product key and ...

Custom LDAP/OPATH query to create custom address list?
I'm going to ask this again because it seems to be a big problem going to exchange 2007. How does one create a custom LDAP query to an external server to build a custom address list? It would be nice to be able to create custom address lists built on LDAP queries to Active Directory or OpenLDAP, etc. Can this be done? Only recipients in Active Directory can appear in Address Lists. You would have to import external recipients into AD as Contacts to be able to do this. -- Bharat Suneja MVP - Exchange www.zenprise.com NEW blog location: exchangepedia.com/blog ------------------------...

BUG: Validating datasets written from a generated strong-typed dataset
I have a pretty simple XSD schema: ******************************************** * XSD SCHEMA ******************************************** <?xml version="1.0" encoding="utf-8" ?> <xs:schema id="PageHitStatistics" targetNamespace="urn:PageHitStatistics" elementFormDefault="qualified" xmlns="urn:PageHitStatistics" xmlns:xs="http://www.w3.org/2001/XMLSchema"> <xs:complexType name="CounterDef"> <xs:attr...

query timing out. how do you stop this from happening?
What can be done to keep a querie from constantly timing out? Almost all my queries and functions are doing this lately as my database grows. Any advice on this is very much welcome. Thanks, MC -- Message posted via AccessMonster.com http://www.accessmonster.com/Uwe/Forums.aspx/access-queries/200705/1 ...

display the result
I have the following programme.module for generating all possiible combination of in a group of 3 items and it works like this Private Sub Command1_Click() Dim rs As ADODB.Recordset Dim stocknames() As String Dim i As Long Dim j As Long Dim k As Long Set rs = New ADODB.Recordset rs.ActiveConnection = CurrentProject.Connection rs.Open "select stockname from stocknames order by stockname", , adOpenForwardOnly, adLockReadOnly i = 0 ReDim stocknames(0) Do While Not rs.EOF ReDim Preserve stocknames(i) stocknames(i) = rs.Fields("stockname") i = i + 1 rs....

Different Type of Picture in a Document
I have received a document that i have to work on that was compiled by various folks and I have no idea who did what. When I select pictures within the document, I have some pictures with the usual single border; circles at the corners, and squares at the edge. I have other pictures, that when selected, have a double border, that is turquoise. There are three dots in a row at the sides and three dots in a triangle at the corner. With this type of picture I cannot explicitly set the size or other options available with the type above. Does anyone know why some pictures would be ...

How do I type in a web-link?
Version: 2004 Operating System: Mac OS X 10.5 (Leopard) Processor: Intel When signing an email, sometimes I want to add my website address as a clickable weblink. I thought that i used to do this by simply typing the address and hitting return, this doesn't seem to work, though. What should I do? It works in Word, but not in Entourage. Links become clickable if the message is viewed by the recipient, but it does not show up as such in the Compose window. If you type your message and hit the Send Later button, you will find the message, complete with clickable hyperlinks, in your outbox. ...

Still get limit warnings after change
I have been working with my E2K3 server to enable more convienient use of the Mail message warning limit. I installed the Quota message service which works very well but when i change a users limits and/or the limits set in the ESM console (mailbox store properties) i am still getting a warning for a user who is now within limits. Is there a lag time? can i manually update something? so the user doesn't receive limit warnings anymore?? Quota changes can take up to two hours to take effect. -- Ed Crowley MVP - Exchange "Protecting the world from PSTs and brick backups!" &q...

How do I get rid of/refresh the browsed pages links in the hyperli
It's driving me nuts just to find one link. Ig I could clear it up once in a while that would be cool. thanks for the help. :) In Internet Explorer, tools, Internet Options, delete files. -- Mary Sauer MSFT MVP http://office.microsoft.com/ http://msauer.mvps.org/ news://msnews.microsoft.com "adumbledore" <adumbledore@discussions.microsoft.com> wrote in message news:04FCEF52-5F12-43CB-B0D1-B093AEA91A23@microsoft.com... > It's driving me nuts just to find one link. Ig I could clear it up once in a > while that would be cool. thanks for the help. :) ...

INSERT TEXTFILE Leading to odd results
Hello All, I am very new at Publisher. I am testing a template that I designed which is a two-sided one-sheet brochure where each side contains three text boxes that I inserted. Each text box is of equal size and I have the overflow links set. Now, when I go to INSERT|TEXTFILE and point to a MS WORD 2003 file that is six pages it all flows into the text boxes, which is fine. The problem is that the formatting within the WORD doc is not preserved. Specifically, I have paragraphs with borders that are squished-up in the PUBLISHER image. Each paragraph is treated as a seperate table, whic...

Running Sum Query
friends, > Q.1: I have table "HMADetail" That has fields as > > Date Lot# SubLot# HMAPlacedToDay HMAPlacedToDate ....... > (1) (2) (3) (4) (5) > > I Have not created Query table from this table but created report with > fields (4) and (5) same. I set property for Field (5) with Running Sum > Overall. This report works good. But it writes all records from begining date > to end date. I need The end date records only. How do I get this without query table? > > Q2: If I have to creat ...

Problem in permanent type add-in toolbar
Hi, I have been facing a issue with dealing with my add-in toolbar. I want to make my toolbar permanent so that use can hide and reposition it permanently(even after restarting Outlook). At the same time, I want to support my add-in toolbar for all opened Outlook explorer. I want to make sure that at any point of time there must be one and only one toolbar on any Outlook window. I have tried below methods but none worked: 1. Tried deleting toolbar on shutdown. This doesn't delete the toolbar and each restarting outlook adds new toolbar. Private Sub AddinInstance_OnBegi...

Help with expression builder in a select Query
I am a beginer of Access. I have a database that consists of 4 columns/fields of dates. These dates/fields can be filled or can be blank. I call them A, B, C, D. Now I want to make a query or report to retrieve the date data in the column/field of A. The criteria for this: all the dates in field A must be later /larger than those in field B or C or D if B or C or D contains any value. I queried the A,B,C,D from the table that contains these fields. The first criteria I wrote is "A is not Null". But then I want to say if B or C or D is not blank, A must be ...

Save as type missing types
What would cause the save as type drop down list not to have any options except for *.xls? ...

Can I change the Excel Queries Developed From Reporting Tools
I have created the Excel iqy queries from the third party reporting tools and during the process there is an option either to pass the User Login id and Pass word as a parameter or not. We have developed some queries using generic id so that the users do not have to pass the Login id and password and now we want to change that. Is there a way to modify that excelquery.iqy file without redoing that. can i see the sql script of that "iqy" file. can i modify that? Please let me know. Thanks in advance!!!! .. ...

Certain e-mails get bounced back to me with the following error.
Please help, Certain contacts I could send to recently now come back undelivered with the following error message. I have looked into the knowledge base and nothing matches. 553 Sorry, that domain isn't allowed (#5.7.1) Thank you in advance for any assistance. Lance You may need to authenticate to your outgoing server, which can be set up on the "Outgoing Server" tab for your account. -- Jeff Stephenson Outlook Development This posting is provided "AS IS" with no warranties, and confers no rights "Lance Stevens" <lance_of_denver@yahoo.com> wr...

Exact match query
My colleague told me that there's a function in sccess that can accomplish this. I need to check every month's report against the last. As I don't want to waste time checking those entries that were checked previously, I want to run a query to find the unmatched entries (comparing 2 tables). However, I don't have a primary key as these tables are imported from excel. I need to find an exact match for every cell in the row. I've tried running the unmatched query on access and it only matched the data in 1 column. How can I go about it? Is there a way to do it in ex...

Saving a Filter as a Query in VB Via a button, Can it be done?
Saving a Filter as a Query in VB Via a button, Can it be done? P.S. I use 07' More info can be found here about my question. http://www.access-programmers.co.uk/forums/showthread.php?t=145785 "JBurlison" wrote: > Saving a Filter as a Query in VB Via a button, Can it be done? P.S. I use 07' ...

getting other folders than inbox synchronized when checking internet mail
I am using Outlook 2002 with Windows XP. can I download mail messages from an internet based account (defined as pop3) that are in a folder other than my inbox? Merrav <anonymous@discussions.microsoft.com> wrote: > I am using Outlook 2002 with Windows XP. > can I download mail messages from an internet based > account (defined as pop3) that are in a folder other than > my inbox? If you want messages received with another account to go into a folder other than Inbox, you'll have to create a rule to move them there. -- Brian Tillman Smiths Aerospace 3290 Patterson A...

GP 10 Web Service - CreateSalesOrder method
I am using the Dynamics GP 10 Web Service, CreateSalesOrder method to create a sales order from my Windows Forms application. I would like to display for the user the Sales Order number that is assigned (automatically) when the order is created. MSDN says the key.id is automatically populated in the SalesOrder class. This may be true, but if so the scope does not extend back to my call because SalesOrder.key.ID is null. The CreateSalesOrder method returns void, so the answer is not there either. I have a kluge answer in which I read the SOP10106 to get back the number of the order just cr...

How do I get PPt 2007 out of safe mode
Just installed PPT2007. Switched an older version presentation to 2007 and it seems to be locked in safe mode. Any ideas on what caused this and how to get it out? Save as a new name "delcoronado" <delcoronado@discussions.microsoft.com> a �crit dans le message de news: 4C54D52D-875B-46CE-8961-386B86D649D7@microsoft.com... > Just installed PPT2007. Switched an older version presentation to 2007 and > it > seems to be locked in safe mode. Any ideas on what caused this and how to > get > it out? Has the trial period expired on the new 2007 i...

HOw do I get the formula to figure out a percentage?
Example H & P Consent Pre-Sedation YES NO YES NO YES NO Total Cases 11 13 12 98 I need the percentage of each column. Assuming that your numbers are from A2 to F2, here is a very simpl solution, i.e., not an elegant formula but will work. If you want the percentages to appear below the numbers, enter in B2 =if(A2="","",A2/sum($A$2+$F$2)) and copy this up to cell F2. Another option is =if(A2="",0,A2/sum($A$2:$F$2)) Also, don't forget to "percent" format your cells (B2:F2). Jen Wrote: > Example > H & P Consent Pr...

Clicking on "Help" in Word 2007 results in a blank help window.
Instead of the online help displaying as it should, the content of the Help window is completely blank. The user reports this happened quite a while back, so doing a System Restore would be shooting in the dark. Is there some registry change to make to get this working as it should? ...

Getting a space between Concatenated cells
HI, When merging data from multiple cells to one cell, is there a way to make a space between the merged text? Example, to have the end cell look like "Bob Jones" instead of "BobJones"? Thanks to anyone who can help! =A1&" "&B1 -- Regards, Peo Sjoblom "Kristine" <kristine@synmarketing.com> wrote in message news:66757CC0-5F12-4CE8-92D8-B9FA8411638E@microsoft.com... > HI, > > When merging data from multiple cells to one cell, is there a way to make a space between the merged text? Example, to have the end cell look like &qu...

I want to get rid of this message!
"The contents of this appointment have been updated." Office 2009, WinXP Please Help! Wendy Oops.....I meant Office 2007! "Wendymel" wrote: > "The contents of this appointment have been updated." > > Office 2009, WinXP > > Please Help! > > Wendy Under what circumstances are you seeing that message? -- -Ben- Ben M. Schorr, MVP Roland Schorr & Tower http://www.rolandschorr.com http://www.officeforlawyers.com/outlook.html Author: The Lawyer's Guide to Microsoft Outlook 2007: http://tinyurl.com/ol4la...