HighTechTalks DotNet Forums  

XPathNavigator - how to tell if a node exists?

Dotnet XML microsoft.public.dotnet.xml


Discuss XPathNavigator - how to tell if a node exists? in the Dotnet XML forum.



Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old   
=?Utf-8?B?RGF2aWQgVGhpZWxlbg==?=
 
Posts: n/a

Default XPathNavigator - how to tell if a node exists? - 07-12-2007 , 04:56 PM






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



Reply With Quote
  #2  
Old   
Bjoern Hoehrmann
 
Posts: n/a

Default Re: XPathNavigator - how to tell if a node exists? - 07-12-2007 , 06:41 PM






* David Thielen wrote in microsoft.public.dotnet.xml:
Quote:
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:bjoern (AT) hoehrmann (DOT) 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/


Reply With Quote
  #3  
Old   
Bjoern Hoehrmann
 
Posts: n/a

Default Re: XPathNavigator - how to tell if a node exists? - 07-12-2007 , 06:41 PM



* David Thielen wrote in microsoft.public.dotnet.xml:
Quote:
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:bjoern (AT) hoehrmann (DOT) 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/


Reply With Quote
  #4  
Old   
=?Utf-8?B?RGF2aWQgVGhpZWxlbg==?=
 
Posts: n/a

Default Re: XPathNavigator - how to tell if a node exists? - 07-12-2007 , 07:08 PM



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:

Quote:
* 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:bjoern (AT) hoehrmann (DOT) 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/


Reply With Quote
  #5  
Old   
=?Utf-8?B?RGF2aWQgVGhpZWxlbg==?=
 
Posts: n/a

Default Re: XPathNavigator - how to tell if a node exists? - 07-12-2007 , 07:08 PM



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:

Quote:
* 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:bjoern (AT) hoehrmann (DOT) 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/


Reply With Quote
  #6  
Old   
Bjoern Hoehrmann
 
Posts: n/a

Default Re: XPathNavigator - how to tell if a node exists? - 07-12-2007 , 07:30 PM



* David Thielen wrote in microsoft.public.dotnet.xml:
Quote:
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:bjoern (AT) hoehrmann (DOT) 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/


Reply With Quote
  #7  
Old   
Bjoern Hoehrmann
 
Posts: n/a

Default Re: XPathNavigator - how to tell if a node exists? - 07-12-2007 , 07:30 PM



* David Thielen wrote in microsoft.public.dotnet.xml:
Quote:
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:bjoern (AT) hoehrmann (DOT) 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/


Reply With Quote
  #8  
Old   
WenYuan Wang [MSFT]
 
Posts: n/a

Default Re: XPathNavigator - how to tell if a node exists? - 07-13-2007 , 07:10 AM



Hello Dave,
Thanks for Bjoern's suggestion.

By the way, have you tied with XPathNavigator.Select method?
This method will return a node set. We could check the count property of
this set.
If there is no node exists, the number will be Zero.

XPathDocument document = new XPathDocument("XMLFile1.xml");
XPathNavigator navigator = document.CreateNavigator();
XPathNodeIterator nodes = navigator.Select("/bookstore/book1");
if (nodes.Count == 0)
{// the node doesn't exist. }
else
{... }

Addiotnally, this method would used with compile.
XPathDocument document = new XPathDocument("XMLFile1.xml");
XPathNavigator navigator = document.CreateNavigator();
XPathExpression expr = navigator.Compile("true()");
switch (expr.ReturnType)
{
case XPathResultType.NodeSet:
{
XPathNodeIterator iterator = navigator.Select(expr);
break;
}
case XPathResultType.Boolean:
{
bool f = (bool)navigator.Evaluate(expr);
break;
}
case XPathResultType.Number:
{
....
break;
}
case XPathResultType.String:
{
...
break;
}
default:
{
...
break;
}
}

Hope this helps. Please feel free to update here if there is anything we
can help with. We are glad to assist you.
Have a great weekend,
Sincerely,
Wen Yuan
Microsoft Online Community Support
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.


Reply With Quote
  #9  
Old   
WenYuan Wang [MSFT]
 
Posts: n/a

Default Re: XPathNavigator - how to tell if a node exists? - 07-13-2007 , 07:10 AM



Hello Dave,
Thanks for Bjoern's suggestion.

By the way, have you tied with XPathNavigator.Select method?
This method will return a node set. We could check the count property of
this set.
If there is no node exists, the number will be Zero.

XPathDocument document = new XPathDocument("XMLFile1.xml");
XPathNavigator navigator = document.CreateNavigator();
XPathNodeIterator nodes = navigator.Select("/bookstore/book1");
if (nodes.Count == 0)
{// the node doesn't exist. }
else
{... }

Addiotnally, this method would used with compile.
XPathDocument document = new XPathDocument("XMLFile1.xml");
XPathNavigator navigator = document.CreateNavigator();
XPathExpression expr = navigator.Compile("true()");
switch (expr.ReturnType)
{
case XPathResultType.NodeSet:
{
XPathNodeIterator iterator = navigator.Select(expr);
break;
}
case XPathResultType.Boolean:
{
bool f = (bool)navigator.Evaluate(expr);
break;
}
case XPathResultType.Number:
{
....
break;
}
case XPathResultType.String:
{
...
break;
}
default:
{
...
break;
}
}

Hope this helps. Please feel free to update here if there is anything we
can help with. We are glad to assist you.
Have a great weekend,
Sincerely,
Wen Yuan
Microsoft Online Community Support
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.


Reply With Quote
  #10  
Old   
=?Utf-8?B?RGF2aWQgVGhpZWxlbg==?=
 
Posts: n/a

Default Re: XPathNavigator - how to tell if a node exists? - 07-13-2007 , 11:52 AM



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:

Quote:
* 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:bjoern (AT) hoehrmann (DOT) 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/


Reply With Quote
Reply




Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.