Connecting Tech Pros Worldwide Forums | Help | Site Map

XmlNode and XPath

JJ
Guest
 
Posts: n/a
#1: Nov 12 '05
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

Oleg Tkachenko
Guest
 
Posts: n/a
#2: Nov 12 '05

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

JJ
Guest
 
Posts: n/a
#3: Nov 12 '05

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]
Oleg Tkachenko
Guest
 
Posts: n/a
#4: Nov 12 '05

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

JJ
Guest
 
Posts: n/a
#5: Nov 12 '05

re: 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]
JJ
Guest
 
Posts: n/a
#6: Nov 12 '05

re: 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]
JJ
Guest
 
Posts: n/a
#7: Nov 12 '05

re: 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]
Oleg Tkachenko
Guest
 
Posts: n/a
#8: Nov 12 '05

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

Oleg Tkachenko
Guest
 
Posts: n/a
#9: Nov 12 '05

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

Oleg Tkachenko
Guest
 
Posts: n/a
#10: Nov 12 '05

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

Closed Thread