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
1763 Views

Similar Articles

[PageSpeed] 30

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:

All Task bars gone How do I get them back?
All of a sudden the other day everything at the top of my screen disappeared. I can load my word documents by searching my files and double clicking on the name but I can not Open a document inside word. I have never seen anything like this where all the buttons for using Word are gone. How do I get them back. http://www.gmayor.com/my_toolbars_are_missing.htm or http://word.mvps.org/FAQs/AppErrors/MissingMenusEtc.htm -- <>>< ><<> ><<> <>>< ><<> <>>< <>><<> Graham Mayor - Word MVP ...

Getting the longest string
XML <data> <option>this is test 1</option> <option>this is test 11</option> <option>this is test 111</option> <option>this is test 1111</option> <option>this is test 11111</option> </data> The 5 option has the longest content. I need to get that length and place in a variable. The test has to look across all of the option nodes and return the longest one. Any suggestions? Cheers Keith Keith Chadwick wrote: > <data> > <option>this is test 1</option> > <opti...

Re: How to get all mails, even those for unknown recipient
Yes, good idea :) It is all i need (for example http://www.christensen-software.com/cscatchall.htm) But i wish those NDR are generated. To inform user that used address might be misstyped Is it posible to combine catchall mailbox with a ability of standard NDR generating? TIA Martin Blackstone - MVP wrote / napisal(a): > I think you are talking a Catch All mailbox. > Check this out > http://mostlyexchange.blogspot.com/2005/04/creating-catch-all-mailbox-in-exchange.html > > > "guzzi" <guzzi@_DOT_yandex.ru> wrote in message news:OoSFMGZhGHA.12...

Change cell background color based on content that results from li
This could be very simple, but I will lay the groundwork first. I have a schedule spreadsheet that I import data to from a web based program. There are existing filters to remove all formatting of the data and remove that which we do not use. What remains is a non formatted sheet that other workbooks link to so we can produce daily sheets. Now the data that is linked on the other sheets may be for example the number 150. It appears throughout the sheet and I would like to color any cell that contains the number 150. The problem is I can't search for 150 because it reall...

Query emails coming to a specific domain
Hello; We have about 5 domains on our exchange server that we receive email from. We are in the process of not renewing one of our domains but would like to check to see if email from that domain is still coming through. Is there any way to do that? I tried the Message Tracking Center with the *.domain.com but it didn't like that. It wanted a specific user. Thanks Check SMTP logs as well. -- Bharat Suneja MVP - Exchange www.zenprise.com NEW blog location: www.exchangepedia.com/blog ---------------------------------------------- <nospam@newsgroup.com> wrote in message news:...

What is the best procedure for Getting a valid COA/License for a XP SP1 system.
I need to find out the best procedure for Getting a valid COA/License for a XP SP1 system. I've checked E-Bay but maybe dealing directly with M/S would be best Bob Robert J. Stevens wrote: > I need to find out the best procedure for Getting a valid > COA/License for a XP SP1 system. > I've checked E-Bay but maybe dealing directly with M/S would be best Not sure you can, anymore - for Windows XP. Assuming you are doing this because it is counterfeit/bad... http://www.microsoft.com/genuine/ -- Shenan Stanley MS-MVP -- How To Ask Questions The Smart ...

Get paid to surf
Surfjunky pays up to .75$ per hour you surf, no limit to how long you surf. I've had an account for 2 days now, and i've let it sit and i've raked in 27$ http://www.surfjunky.com/?r=Etra1n ...

How to get device name?
Hello all: I want to know all the name of devices in my PC, because I want to open them using API CreateFile and test it, how to do?thanks. Hi, what you want do by meanin "test it"? You cant open all devices with the API CreateFile (for several reasons). Regards Kerem -- ----------------------- Beste Gr�sse / Best regards / Votre bien devoue Kerem G�mr�kc� Latest Open-Source Projects: http://entwicklung.junetz.de ----------------------- "Lee Tow" <fbjlt@pub3.fz.fj.cn> schrieb im Newsbeitrag news:#vVH#JkqKHA.4220@TK2MSFTNGP0...

I have a problem with getting a total Sum of numbers (URGENT)
I'd really appreciate if someone could help me here. I am using Microsoft Excel 2002 on a Microsoft Office XP system. I a trying to develop a minor league baseball schedule, but am having som problems. I have attached a copy of my spreadsheet to give everyone some idea o what I'm doing here. I have where the fans list the "RUNS, HITS & ERRORS) of both teams an for each game of the series. I then have it set up in the TOTA WON/LOST column where if the T-Bones RUNS are more than the SALTDOG runs, a "1" appears in the WON column. If the SALTDOGS have a highe numbe...

Excel 2007. How do I get un-installed add-ins off the toolbar?
I have 2 Large Icons on the ribbon/toolbar of Excel 2007. These are Omnipage and s-force. Both of these have been uninstalled and I have de-activated both add-ins. How do I get these off the toolbar forever? thanks ...

Getting back a deleted test frame after saving the changes
My son just deleted a text frame with all his work and started a new text frame and then when asked if he wanted to save his work he clicked yes. Is there any way to retrieve that missing text frame when it won't allow you to undo? On Sat, 27 Aug 2005 16:04:01 +0100, Denise wrote (in article <09056FDE-03FC-4988-8AB0-D1EA0896BFC7@microsoft.com>): > My son just deleted a text frame with all his work and started a new text > frame and then when asked if he wanted to save his work he clicked yes. Is > there any way to retrieve that missing text frame when it won't a...

How do I get a copy of Publisher 2002?
I'd like to get a copy of Publisher 2002. I still have Windows 98 on my computer and don't want to upgrade to XP right at this time. Other than ordering off of e-bay or other sites I'm not familiar with, is it possible to get a copy of Publisher 2002 that would be compatible with Windows 98? Amazon.com -- Don Vancouver, USA "Crystal1365" <Crystal1365@discussions.microsoft.com> wrote in message news:33518C70-6D02-4E03-B9C2-2C1FDEEB4A68@microsoft.com... > I'd like to get a copy of Publisher 2002. I still have Windows 98 on my > computer and don...

Can't get at downloaded transactions
While Money was going through it's online update I also was in the process of requesting a download of transactions from a bank which uses web statements rather than direct statements. Somehow things must have gotten out of sync so that now Money is showing I have 19 transactions from this institution to review (which is correct) but it doesn't present me with anything to review when I'm in that account. I've tried re-downloading from the bank but that doesn't help. Any ideas how I can access the downloaded transactions? In microsoft.public.money, Ken wrote: &...

How do i get send/receive back
-- Peter Please reply to Newsgroup for the benefit of others. "Andrea" <Andrea@discussions.microsoft.com> wrote in message news:DA7F81D0-A97A-4265-8676-82A0CDC143FA@microsoft.com... > ...

Automatically updating cells on other worksheets when data typed into primary sheet
I'm trying to create a workbook that will allow me to enter data on the first sheet and it will then append the data to a second sheet by automatically starting a new line. Example: I enter "This is a test" into A1 on sheet 1 First available line on sheet2 automatically updates with "This is a test" When it comes to enter something new in A1 on sheet1, same function above goes into action, accept it should be on the next available line which will be A2 on sheet2, as A1 is already used by previous data entry. Can this be done? I would appreciate any help with this. ...

getting consolidate list of "follow up" items
Dear All, I would like to get a consolidated list of items (sent and received mails)that have matured - i.e. whose follow up time is up. I have the habit of tagging a sent mail (after actually sending) for follow up. Similarly, mails received are also tagged for my further action (for follow up). Would appreciate a consolidated follow up report generation way - rather that opening each folder and looking for mature items (i.e. red). Thanks in advance. Sanjay ...

How to get Row # in formula?
I have several rows with the same formula but they are seperated, in some cases, by rows with other information to prevent me from just dragging a formula down to other rows. Ex. While in row 13: B13*C13+B13*D13+B13*E13 While in row 20: B20*C20+.... how can I write a formula to populate the "13" or "20" automatically for whatever row I am in? Thanks, Dave You may not be able to drag it, but if you copy row13 formula and go to row 20 it updates does it not? -- HTH Bob "DaveR" <DaveR@discussions.microsoft.com> wrote ...

query to make a list of products based on delivery history
I have a table which lists all deliveries made of our product. From this table, I'd like to make a list of all products. My problem is the products will have many duplicates as they can be ordered multiple times and I just want a list showing all the individual products that we offer. -- TIA Try something like: SELECT DISTINCT [our product] FROM [which list all deliveries made]; If you can't figure this out, come back with table and field names. -- Duane Hookom Microsoft Access MVP If I have helped you, please help me by donating to UCP http://www.access.hookom.net/UCP/Def...

Lookup returning incorrect results
Anybody have an idea why I keep getting incorrect lookup results. In sheet 1, I have a list of "Sales Codes" in col A. In sheet 2, I have a list of data that with column A for "Sales Codes" and column B for associated "Sales Reps". I want to insert a column in sheet 1 that contains lookup formulas to pull the correct sales rep for each sales code . Both sheets are sorted in ascending order by Sales code. My lookup formula in sheet 1, Lookup col B: =LOOKUP(A2,Sheet2!$A$2:$B$4,Sheet2!$B$2:$B$4) The returned results from the above lookup formula are incorrect....

automatically get an alert when someone adds an event to public Calendar?
Is there a way to automatically get an alert when someone adds an event to a public Calendar? Having the person who added the event send me an invite is not an option, because they will not do this. (I am required to check the calendar to see if any events have been added.) I can't see a way to quickly see what new events have been added so I have to check the whole calendar. (Or is there a way to view events by the date they were added, not the date of the event?) TJ There should be some solutions for this at = http://www.slipstick.com/addins/notify.htm=20 --=20 Sue Mosher, Outlook...

Update Query based on Current Recordset
I have a continuous form with filtered records. I want to run an update query on a field in a table based on the current filtered records on the form. What code would I use to accomplish this task? Any help appreciated in getting me started. NEWER USER, Without a wee bit more information, like what are you trying to update, field names, table names, do you want this attached to a button or an event, etc... The best I can do is... UPDATE SomeTable SET SomeTable.SomeField= WhateverValue WHERE (((SomeTable.SomeOtherFied)=WhateverValue)); Of course, the above would be an ...

Web hyperlinks: default browser and resulting browser window size?
Hi, Am using Excel 97 (I know it is old but I like it!) with Windows XP Pro (SP2). Have a series of web hyperlinks in a worksheet, that load the current chart for a specified chart, from Yahoo Finance. For example: http://chart.finance.yahoo.com/c/1y/c/tls.ax will load the chart for the Australian telco "Telstra". All the links work just fine but have two questions: 1) My default internet browser is Firefox but the links always open Internet Explorer. Is there a way to ensure that Firefox is used? 2) The loaded chart appears in a maximised IE window. Which is a bit silly as t...

Can't Get Moneycentral to Load
I download the custom or higher level version and I can't see the chart. Just get a placeholder where the chart should be. Running Win98. Used to run it before, but not now. Anyone have any ideas? Is it some graphics setting I'm missing? Thanks. "Question Quigley" <Quigley-at-limerick-dot-ie> wrote in message news:YNnjg.4959$hN2.1745@trnddc05... > > I download the custom or higher level version and I can't see the chart. > Just get a placeholder where the chart should be. > > Running Win98. Used to run it before, but not now. > > Any...

How to get task manager CPU and default memory column values
I need to return CPU and memory columns for each process in server from web service like task manager shows by default. I tried code below but s.TotalProcessorTime throws Win32exception with stack below. How to get CPU and real memory size from server using web service ? Andrus. static void Processes(StringBuilder sb) { List<Process> processes = new List<Process>(); foreach (Process process in Process.GetProcesses()) processes.Add(process); processes.Sort((a, b) => { ...

Getting the handle of a combo box in a dialog
have a dialog box that contains a tool bar that contains a combo box. From the dialog box I want to obtain the combo box control to perform a SelectString(). I know that I need to get a handle for it first. The reason I am doing it this way is because when I created the tool bar I placed an empty button there and named it IDC_FONTNAME and then in my tool bar class, I did a SetButtonInfo and changed the style to a TBBS_SEPARATOR. I then created the combo box using the IDC_FONTNAME From the dialog class. CComboBox* pCB = (CComboBox*)GetDlgItem(IDC_FONTNAME); //// returns 0x00000000...