Connecting Tech Pros Worldwide Help | Site Map

XmlNode and XPath

 
LinkBack Thread Tools Search this Thread
  #1  
Old November 12th, 2005, 01:11 AM
JJ
Guest
 
Posts: n/a
Default XmlNode and XPath

Hi All,

I noticed that XmlNode and XpathNavigator are quite
similiar. XmlNode seems to navigate over an XML Doc and
so does XPathNav so when do I use XPathNavigator instead
of XmlNode?

Thanks,

JJ

  #2  
Old November 12th, 2005, 01:11 AM
Oleg Tkachenko
Guest
 
Posts: n/a
Default Re: XmlNode and XPath

JJ wrote:
[color=blue]
> I noticed that XmlNode and XpathNavigator are quite
> similiar. XmlNode seems to navigate over an XML Doc and
> so does XPathNav so when do I use XPathNavigator instead
> of XmlNode?[/color]

XPathNavigator is unified XPath selection facility. It allows navigate
over any XPathNavigable stores, including XmlDocument. In fact
SelectNode method's using XPathNavigator within.
Hint: XmlDocument ios not only XML API in .NET, learn about
XPathDocument, it's gonna shadow XmlDocument soon.
--
Oleg Tkachenko
XML Insider
http://www.tkachenko.com/blog

  #3  
Old November 12th, 2005, 01:11 AM
JJ
Guest
 
Posts: n/a
Default Re: XmlNode and XPath

I am trying to work with XPathDocument in my code but I
tryed to assign it like the following and get an error
that it can't cast to a XmlNode. Here's the code:

XPathDocument xd = new
XPathDocument(@"D:\XML\XSL Ex2\Personnel.xml");

XPathNavigator nav = xd.CreateNavigator();

XslTransform xslt = new XslTransform();
xslt.Load(@"D:\XML\XSL Ex2\Personnel2.xsl");
StringWriter fs = new StringWriter();
xslt.Transform(xd, null, fs, null);

//select the root node
nav.MoveToRoot();


XmlNode node = nav.Select("Emps"); <- It errors
here

What do u think Oleg?

JJ

[color=blue]
>-----Original Message-----
>JJ wrote:
>[color=green]
>> I noticed that XmlNode and XpathNavigator are quite
>> similiar. XmlNode seems to navigate over an XML Doc[/color][/color]
and[color=blue][color=green]
>> so does XPathNav so when do I use XPathNavigator[/color][/color]
instead[color=blue][color=green]
>> of XmlNode?[/color]
>
>XPathNavigator is unified XPath selection facility. It[/color]
allows navigate[color=blue]
>over any XPathNavigable stores, including XmlDocument.[/color]
In fact[color=blue]
>SelectNode method's using XPathNavigator within.
>Hint: XmlDocument ios not only XML API in .NET, learn[/color]
about[color=blue]
>XPathDocument, it's gonna shadow XmlDocument soon.
>--
>Oleg Tkachenko
>XML Insider
>http://www.tkachenko.com/blog
>
>.
>[/color]
  #4  
Old November 12th, 2005, 01:11 AM
Oleg Tkachenko
Guest
 
Posts: n/a
Default Re: XmlNode and XPath

JJ wrote:
[color=blue]
> I am trying to work with XPathDocument in my code but I
> tryed to assign it like the following and get an error
> that it can't cast to a XmlNode. Here's the code:[/color]
XPathDocument is read-only, it's XML querying facility. If you need
editing - use XmlDocument.
At least till .NET 1.2.
--
Oleg Tkachenko
XML Insider
http://www.tkachenko.com/blog

  #5  
Old November 12th, 2005, 01:11 AM
JJ
Guest
 
Posts: n/a
Default XmlNode and XPath

Oleg,

Here is the code of an XmlNode :

XmlNode node;
if(node.Attributes.Count>0){}

Using XPathNav what is its equivalent?

Thanks,

JJ




[color=blue]
>-----Original Message-----
>Hi All,
>
> I noticed that XmlNode and XpathNavigator are quite
>similiar. XmlNode seems to navigate over an XML Doc and
>so does XPathNav so when do I use XPathNavigator instead
>of XmlNode?
>
>Thanks,
>
>JJ
>.
>[/color]
  #6  
Old November 12th, 2005, 01:11 AM
JJ
Guest
 
Posts: n/a
Default XmlNode and XPath

Oleg,

In your reply about the casting error, all I was trying
to do was assign a selected node from XpathNav to an
XmlNode, not vise versa, so I don't quite understand why
it failed?

- XPathNodeIterator Question -
Also I noticed that in my readings that when I use
XPathNavigator, I see mentioning of using a
XPathNodeIterator :

XPathNodeIterator ni = nav.Select(expr)

This seems to allow me to go and query though a selected
set of Nodes.

So I should also be using this to get at any particular
node?

Thanks,

JJ

[color=blue]
>-----Original Message-----
>Hi All,
>
> I noticed that XmlNode and XpathNavigator are quite
>similiar. XmlNode seems to navigate over an XML Doc and
>so does XPathNav so when do I use XPathNavigator instead
>of XmlNode?
>
>Thanks,
>
>JJ
>.
>[/color]
  #7  
Old November 12th, 2005, 01:11 AM
JJ
Guest
 
Posts: n/a
Default XmlNode and XPath

One more thing Oleg,

I not sure but I thought in order to use XSLTranforms,
I needed to use an XpathDocument? If I can use
XMLDocument instead and attach an XSLT sheet that would
be great too. If I can use XSLT with XMLDocument what is
the command to assign it to xmldocument?

Thanks for all your Help!!!

JJ


[color=blue]
>-----Original Message-----
>Hi All,
>
> I noticed that XmlNode and XpathNavigator are quite
>similiar. XmlNode seems to navigate over an XML Doc and
>so does XPathNav so when do I use XPathNavigator instead
>of XmlNode?
>
>Thanks,
>
>JJ
>.
>[/color]
  #8  
Old November 12th, 2005, 01:11 AM
Oleg Tkachenko
Guest
 
Posts: n/a
Default Re: XmlNode and XPath

JJ wrote:[color=blue]
> I not sure but I thought in order to use XSLTranforms,
> I needed to use an XpathDocument? If I can use
> XMLDocument instead and attach an XSLT sheet that would
> be great too.[/color]

You can use both XmlDocument and XPpathDocument as input for
transformation, but beware XPathDocument is faster as it's optimized
exactly for XPath and is read-only. Use XPathDocument if you don't need
to edit XML in-memory.
[color=blue]
> If I can use XSLT with XMLDocument what is
> the command to assign it to xmldocument?[/color]
There is no such command. Instead take a look at
XslTransform.Transform() method.
--
Oleg Tkachenko
http://www.tkachenko.com/blog
Multiconn Technologies, Israel

  #9  
Old November 12th, 2005, 01:11 AM
Oleg Tkachenko
Guest
 
Posts: n/a
Default Re: XmlNode and XPath

JJ wrote:
[color=blue]
> Here is the code of an XmlNode :
>
> XmlNode node;
> if(node.Attributes.Count>0){}
>
> Using XPathNav what is its equivalent?[/color]

if (nav.MoveToFirstAttribute()) {
...
//Get back to element
nav.MoveToParent();
...
}

XPathNavigator is cursor-like API, which should be familiar for DBMS
oriented heads. Using it you are *navigating* over XML tree.
--
Oleg Tkachenko
XML Insider
http://www.tkachenko.com/blog

  #10  
Old November 12th, 2005, 01:11 AM
Oleg Tkachenko
Guest
 
Posts: n/a
Default Re: XmlNode and XPath

JJ wrote:
[color=blue]
> This seems to allow me to go and query though a selected
> set of Nodes.
>
> So I should also be using this to get at any particular
> node?[/color]

Sure. But only if you are navigating over XmlDocument.
To get selected XmlNode you should use IHasXmlNode interface:
XmlNode node = ((IHasXmlNode)ni.Current).GetNode();
--
Oleg Tkachenko
XML Insider
http://www.tkachenko.com/blog

 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,662 network members.