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
  #11  
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
  #12  
Old   
=?Utf-8?B?RGF2aWQgVGhpZWxlbg==?=
 
Posts: n/a

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






Tried that but it fails if the xpath is "name(/root/node)".

Bjoern's idea looks like it will work so we're going that route.

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

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




"WenYuan Wang [MSFT]" wrote:

Quote:
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
  #13  
Old   
=?Utf-8?B?RGF2aWQgVGhpZWxlbg==?=
 
Posts: n/a

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



Tried that but it fails if the xpath is "name(/root/node)".

Bjoern's idea looks like it will work so we're going that route.

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

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




"WenYuan Wang [MSFT]" wrote:

Quote:
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
  #14  
Old   
=?Utf-8?B?RGF2aWQgVGhpZWxlbg==?=
 
Posts: n/a

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



wait - I didn't look closely enough at what you put - yes that is exactly
what we are going to do.

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

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




"WenYuan Wang [MSFT]" wrote:

Quote:
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
  #15  
Old   
=?Utf-8?B?RGF2aWQgVGhpZWxlbg==?=
 
Posts: n/a

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



wait - I didn't look closely enough at what you put - yes that is exactly
what we are going to do.

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

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




"WenYuan Wang [MSFT]" wrote:

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