473,385 Members | 1,445 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

XPathNavigator - how to tell if a node exists?

Hi;

If I have an XPathNavigator object and for a given xpath statement need to
know if the node exists, how should I do this? I have found for some xpath
functions it returns an empty string if the node does not exist but that is
also the result for an empty node.

--
thanks - dave
david_at_windward_dot_net
http://www.windwardreports.com

Cubicle Wars - http://www.windwardreports.com/film.htm
Jul 12 '07 #1
4 8749
* David Thielen wrote in microsoft.public.dotnet.xml:
>If I have an XPathNavigator object and for a given xpath statement need to
know if the node exists, how should I do this? I have found for some xpath
functions it returns an empty string if the node does not exist but that is
also the result for an empty node.
I do not know how you use the XPathNavigator, but using methods that
return strings is not the right approach; also note that if your XPath
expression evaluates to a string, there is no way to determine whether
some nodes relevant for the expression exist or not. A simple method is
to use nav.Evaluate("boolean(...)") and cast the result to a boolean.
--
Björn Höhrmann · mailto:bj****@hoehrmann.de · http://bjoern.hoehrmann.de
Weinh. Str. 22 · Telefon: +49(0)621/4309674 · http://www.bjoernsworld.de
68309 Mannheim · PGP Pub. KeyID: 0xA4357E78 · http://www.websitedev.de/
Jul 12 '07 #2
The problem with that is a user can feed us anything for the xpath -
including true(). What we really need is something that returns any of the
following:
1) not a legit xpath statement
2) xpath to a node - node exists
3) xpath to a node - node does not exist
4) other - like name(/root/node)

--
thanks - dave
david_at_windward_dot_net
http://www.windwardreports.com

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


"Bjoern Hoehrmann" wrote:
* David Thielen wrote in microsoft.public.dotnet.xml:
If I have an XPathNavigator object and for a given xpath statement need to
know if the node exists, how should I do this? I have found for some xpath
functions it returns an empty string if the node does not exist but that is
also the result for an empty node.

I do not know how you use the XPathNavigator, but using methods that
return strings is not the right approach; also note that if your XPath
expression evaluates to a string, there is no way to determine whether
some nodes relevant for the expression exist or not. A simple method is
to use nav.Evaluate("boolean(...)") and cast the result to a boolean.
--
Björn Höhrmann · mailto:bj****@hoehrmann.de · http://bjoern.hoehrmann.de
Weinh. Str. 22 · Telefon: +49(0)621/4309674 · http://www.bjoernsworld.de
68309 Mannheim · PGP Pub. KeyID: 0xA4357E78 · http://www.websitedev.de/
Jul 12 '07 #3
* David Thielen wrote in microsoft.public.dotnet.xml:
>The problem with that is a user can feed us anything for the xpath -
including true(). What we really need is something that returns any of the
following:
1) not a legit xpath statement
2) xpath to a node - node exists
3) xpath to a node - node does not exist
4) other - like name(/root/node)
You can use the .Compile method which will return a XPathExpression
object which has a returnType property of type XPathResultType which
in turn will allow you to distinguish these cases. Evaluating the ex-
pression will then tell you whether there are matching nodes.
--
Björn Höhrmann · mailto:bj****@hoehrmann.de · http://bjoern.hoehrmann.de
Weinh. Str. 22 · Telefon: +49(0)621/4309674 · http://www.bjoernsworld.de
68309 Mannheim · PGP Pub. KeyID: 0xA4357E78 · http://www.websitedev.de/
Jul 12 '07 #4
great idea - thanks
--
thanks - dave
david_at_windward_dot_net
http://www.windwardreports.com

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


"Bjoern Hoehrmann" wrote:
* David Thielen wrote in microsoft.public.dotnet.xml:
The problem with that is a user can feed us anything for the xpath -
including true(). What we really need is something that returns any of the
following:
1) not a legit xpath statement
2) xpath to a node - node exists
3) xpath to a node - node does not exist
4) other - like name(/root/node)

You can use the .Compile method which will return a XPathExpression
object which has a returnType property of type XPathResultType which
in turn will allow you to distinguish these cases. Evaluating the ex-
pression will then tell you whether there are matching nodes.
--
Björn Höhrmann · mailto:bj****@hoehrmann.de · http://bjoern.hoehrmann.de
Weinh. Str. 22 · Telefon: +49(0)621/4309674 · http://www.bjoernsworld.de
68309 Mannheim · PGP Pub. KeyID: 0xA4357E78 · http://www.websitedev.de/
Jul 13 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Bruce Dunwiddie | last post by:
I'm trying to build a couple classes that would allow for writing xsl transforms against data that is not originally xml. I've got an xmlreader implementation that seems to work well. Based on some...
7
by: David Thielen | last post by:
Hi; Is there a way from an XPathNavigator object to get an xpath string that will, when used in a Select(xpath) on the underlying base/root XPathNavigator return the same XPathNavigator? In...
6
by: David Thielen | last post by:
Hi; How can I tell if I can call XPathNavigator.ValueAsBoolean safely - without throwing an exception. I want to take one course of action if the value can be interpreted as a boolean. But I...
12
by: David Thielen | last post by:
Hi; I have an element: <space> </space> When I call SelectSingleNode() on it, the InnerXml is a 0 length String, not a String containing 1 space. Any ideas?
0
by: br | last post by:
The following is the part of the code I have written.. XPathNavigator oNav = oDoc.CreateNavigator(); XPathNodeIterator oIterator = oNav.Select ("//countries/country"); while...
2
by: JSheble | last post by:
I'm integrating with a customer application, and in their assembly, when I call a method it returns data to us as an XPathNavigator object. I can parse the various elements I need out of this...
4
by: Jack Fox | last post by:
I'm navigating through a XPathDocument consisting of mostly mixed complex type elements (i.e. innertext plus more elements). When I use XPathNavigator.Value to retrieve the text I get everything...
0
by: kelvin273 | last post by:
Hi all, i'm new in this community (and in .NET programming) and i've a problem with xpathnavigator. The idea is to have an xml document with attribute editable by windows form. I write this...
4
by: =?Utf-8?B?a2FydXpv?= | last post by:
Hi, I cannot guess, why some XPath expressions in my code returns false and some even errors, while the others returne true as I expected. I'am workin with VS 2008 Express Edition. Dim xEl As...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.