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
0
anonymous (74722)
12/9/2003 8:12:31 PM
dotnet.xml 7266 articles. 0 followers. Follow

9 Replies
596 Views

Similar Articles

[PageSpeed] 49

JJ wrote:

>   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?

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

0
Oleg
12/9/2003 8:25:18 PM
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


>-----Original Message-----
>JJ wrote:
>
>>   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?
>
>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
>
>.
>
0
anonymous (74722)
12/9/2003 8:33:26 PM
JJ wrote:

> 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 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

0
Oleg
12/9/2003 8:40:21 PM
Oleg,

  Here is the code of an XmlNode :

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

   Using XPathNav what is its equivalent?

Thanks,

JJ





>-----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
>.
>
0
anonymous (74722)
12/9/2003 8:41:37 PM
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


>-----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
>.
>
0
anonymous (74722)
12/9/2003 8:52:51 PM
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



>-----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
>.
>
0
anonymous (74722)
12/9/2003 8:56:39 PM
JJ wrote:
>   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. 

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.

> If I can use XSLT with XMLDocument what is 
> the command to assign it to xmldocument?
There is no such command. Instead take a look at 
XslTransform.Transform() method.
-- 
Oleg Tkachenko
http://www.tkachenko.com/blog
Multiconn Technologies, Israel

0
Oleg
12/9/2003 10:13:55 PM
JJ wrote:

>   Here is the code of an XmlNode :
> 
>    XmlNode node;
>    if(node.Attributes.Count>0){}
> 
>    Using XPathNav what is its equivalent?

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

0
Oleg
12/10/2003 7:35:27 AM
JJ wrote:

> 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?   

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

0
Oleg
12/10/2003 7:38:13 AM
Reply:

Similar Artilces:

XmlNode
When an XmlNode method returns an XmlNode, such as a child, is this generated on-the-fly or is it the actual component of the internal tree structure? If the parent is moved in the tree, will the pointer remain valid? I have looked at as much of the structure as I can using .NET Reflector and I think it will, but I want to be sure. I 'm planning to add XmlNodes to the Tag property of TreeNodes in a TreeView to be able to persist changes back to the document and I would like to know if this will take up any more memory than when they are not linked in this way, or if it is better to...

xmlNode.InnerText vs. xmlNode.Value
I've loaded the following xml into an XMLDocument object: <?xml version="1.0" ?> - <Preferences> <Institution>Argh</Institution> <Speaker>Chigier, Ben (1234)</Speaker> <Worktype>Addendum</Worktype> <speakerCode>1234</speakerCode> </Preferences> I then try to retrieve the value of the Institution node as follows: string s = myxmldoc.SelectSingleNode("/Preferences/Institution").Value; However, s is null after the call. But if I change the call to the following, s is what I'd expect: "A...

XPath peformance vs Hash
How does XPath performance compare to accessing a Hashtable structure? I will need to access the same data repeatedly throughout the lifecycle of my application. What factors should I take into consideration? Where do you have your data stored? Are you trying to trade off between storing it in XML and query using XPath vs storing it using a HashTable in code? Hasttable is going to give you better performance, but is not going to be very flexible. Given you have the same data always, Hashtable is the better approach though. "Ben" <benjaminsleek@gmail.com> wrote in messag...

How to get the XML path of an XmlNode?
How can I get the full XML path (as string) of a specific XmlNode ? -- Thanks Sharon Sharon wrote: > How can I get the full XML path (as string) of a specific XmlNode ? What is "the full XML Path" of a node, do you have any definition for that? Some XML editors can assist you constructing or maybe even generating an XPath expression. -- Martin Honnen --- MVP XML http://JavaScript.FAQTs.com/ Lets say I have the following XML file: <?xml version="1.0" encoding="utf.8"?> <System Name = "System"> <Parameters> <T1&...

XmlSerialization to XmlNode
I have a schema that I load into classes that mirror the schema using the XmlSerialization. The schema contains an xsd:Any that I load into 'Extra' as follows: [XmlElement(ElementName="Extra")] public XmlNode Extra { get { return extra;} set { extra = value;}} However, if I have: <Something> <Extra> <a>something</a> <b>another thing</b> </Extra> </Something> I would expect Extra to contain an XmlNode of : <Extra> <a>something</a> <b>another thing</b> <...

How do I write a string in the destination element depending on what the string is in the source, using XSLT/XPath? (Like a switch statement.)
Is there something like a switch statement in XPath? What I want to do is this: In the source there is an element called "member" with either the value "0" or "1". In the destination element (also called "member") the value should be "No" or "Yes" (Where "0" corresponds to "No" and "1" to "Yes"). How do I do this? Joakim Olesen wrote: > Is there something like a switch statement in XPath? What I want to do is > this: > > In the source there is an element called "member&quo...

Function to escape arguments for an xpath / xquery expression?
Hi, I'm using MS Sql Server 2005. I have a clause in my query that looks like OR (sae.category_xml.exist('/categories/info_type/category/ @name[contains(.,"$keyword$")]') = 1) where "$keyword$" is replaced with the string I want to check. However, if that string contains a "&", the expression fails with an "Unknown entity reference" error. Does anyone know of a function I can use that will escape my string to substitute all the appropriate entity references? Thanks, - Dave On Wed, 18 Aug 2010 10:02:49 -0700 (PDT), la...

Problem with XmlNode.ChildNodes across different machines
I'm having the darndest XML config file problem that I really need help with. I'm supporting a .NET 1.1 desktop application with its own config file, and I implement IConfigurationSectionHandler so I can have a custom config section in my config file. The IConfigurationSectionHandler.Create method just returns the 'section' XmlNode back to the consumer. The config file looks kind of like this: <?xml version="1.0" encoding="utf-8" ?> <configuration> <configSections> <section name="MyConfigSettings" type="MyAssembl...

Complex xPath Query #2
I am trying to build a xPath Query to use on SelectNodes function in VB.net The data I want to search is by MemberCity = O'Fallon My xPath Query is xPathQuery = "Matched[@MemberCity='O'Fallon']" The problem is the apostrophe in the city name that is failing. How do I build the xPathQuery so that it can take into account the apostrophe Any idea(s) thanks --- Posted using Wimdows.net NntpNews Component - Post Made from http://www.DotNetJunkies.com/newsgroups Our newsgroup engine supports Post Alerts, Ratings, and Searching. ...

how to transform escaped xml contents in xmlnode
Hi, I have an XML document, created from ADO DataSet, which contains XML data in some nodes shown below: <NewDataSet> <Table> <field_name>My Selection</field_name> <field_type>dropdown</field_type> <field_choice_text> &lt;Choices&gt;&lt;Choice choice_text=\"xxx\" /&gt;&lt;Choice choice_text=\"yyy\" /&gt;&lt;Choice choice_text=\"zzz\" /&gt;&lt;/Choices&gt; </field_choice_text> <field_answer> xxx </field_answer> </Table> </NewDataSet> How can I get ...

Trouble with XPath query #2
I have a query where i need to look for a value of a lower level xml element based on the value of a parent element existing first. Here is a simple example of the xml <S3Client> <Buttons> <Button>Activity <RestrictedClientType> <ClientType>02</ClientType> </RestrictedClientType> </Button> ... </Buttons> </S3Client> Here is my query, I am trying see if ClientType '02' exists when the Button element has a value of "Activity&quo...

xmlnode question
Hello, I'm new to xml and I have a question: I try to do the following Dim doc As New XmlDocument doc.LoadXml("<Request><Root><Requestor name='' /><FlowData flowstatus='' /><Products><productdate productid='' /></Products></Root></Request>") Dim node As XmlNode = doc.SelectSingleNode("Request/Root/Requestor/@name") node.Value = "TestValue" Dim node3 As XmlNode = doc.SelectSingleNode("Request/Root/FlowData/@flowstatus") node3.Value = &q...

What is the difference between XmlNode and XmlElement?
What is the difference between XmlNode and XmlElement? I'm trying to implement the code at http://www.devx.com/dotnet/Article/11616/0/page/3 in C# and I'm getting an error at this line: this.appSettingsElement = this.xml.SelectSingleNode("//configuration/appSettings"); Error: Cannot implicitly convert type 'System.Xml.XmlNode' to 'System.Xml.XmlElement' Thanks. Pluto wrote: > What is the difference between XmlNode and XmlElement? The same as between fruit and apple. XmlElement is particular type of XmlNode. > this.appSettingsElement = > this....

Getting Element By XPATH
I have following XML <Root> <dtTable> <ID>1</ID> <Name>blah</Name> </dtTable> <dtTable> <ID>2</ID> <Name>blah</Name> </dtTable> <dtTable> <ID>3</ID> <Name>blah</Name> </dtTable> </Root> Can I get the Name for ID =>3 .....with this XML Structure using XPATH..if not what is the perfect way to do it Raed Sawalha wrote: > I have following XML > > <Root> > <dtTable> > <ID>3</ID> > <Name>blah</Name> > </dtTab...

XmlNode.Value
Can anyone from M$ or anyone else for that matter explain to me the reasoning behind the intended usage for this property? I was quite surprised to find that this property may be null depending on the XmlNodeType. If one imagines a scenario where some hapless programmer adds a new node to an XML-document, e.g. childNode = doc.CreateNode (XmlNodeType.Element, "foo", ""); and then decides to set the *value* (the "string" between <nodename> and </nodename>) of this node, one could easily imagine that our programmer would look for a method or property ca...

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 JJ wrote: > 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? XPathNavigator is unified XPath selection facility. It allows navigate over any XPathNavigable stores, including XmlDocument. In fact SelectNode method's using XPathNavigato...

Getting attribute value from xml using xpath to get file path
I have the following xml file: <?xml version="1.0" encoding="utf-8" ?> <course id="2555" title="Developing Microsoft .NET Applications for Windows (Visual C# .NET)" length="5 days" source="http://www.microsoft.com/learning/syllabi/en-us/2555Afinal.mspx"> <module id="1" title="Introducing Windows Forms" location="D:\Disk-C\Documents and Settings\orit_itzhar.ATRICA\My Documents\XML\csharp"> <lesson id="1.1"> <subject>Creating a Form</subject> <f...

XmlNode.HasChildNodes
I have the following issue. I'm trying to read to following XML doc. <Main> <Submain> <SubField1/> <SubField2/> </Submain> <Field1/> <Field2>AnyTextValue</Field2> </Main> I loop using foreach XmlNode and testing the HasChildNodes property. When I get to the <Submain> tag the HasChildNodes property is True When I get to the <Field1/> tag the HasChildNodes property is False so far it's clear. Then... When I get to the <Field2> tag the HasChildNodes property is True???!!! Is it correct that the ...

With an XmlNode from a NodeList Can you tell where you are in the original XmlDocument
This problem was raised in another post Assuming we have an XmlDocument (xDoc) that has a node (<dickster>) randomly dispersed throughout the hierarchy. eg <root> <A> <B> <C> <dickster>1</dickster> </C> </B> </A> <D> <dickster>2</dickster> </D> <E> <F> <dickster>3</dickster> <dickster>4</dickster> </F> </E> <dickster>5</dickster> </root> We use SelectNodes to get an XmlNodeList that mathce...

XPath MaFan
Isn't this a bit non-clever, that IDs in the second level are not accessible like that? After all attributes can never have children, so why not checking this in the xpath processer. Place the code in VB6 with MSXML referenced and find out that the second xdn node is nothing. Not sensible, MS! Dim xdd As MSXML2.DOMDocument Set xdd = New MSXML2.DOMDocument Call xdd.loadXML("<Root> <Item ID=""890F4055-E3BE-4BCF-B342-7F6F08DFB365""> <Item ID=""F7692D30-3A6B-4C2C-99C0-00C8B17E36E8""> </Item> </Item> <Item ID=&q...

XmlDocument and XmlNode
Hi, I've loaded an XmlDocument from a file XML: this file has many similar XmlNode and it is difficult to select them by using SelectSingleNode of the object XmlDocument. I've this question: is it possible to identify each XmlNode of the XmlDocument by using an inner key or something like that ? I precise that I 'm not allowed to change the file XML, therefore I can't add any key element or attribute in each node. Thank in advance Siu wrote: > I've loaded an XmlDocument from a file XML: this file has many similar > XmlNode and it is difficult to select the...

XPath: Is there a better way to do this...
I am retrieving XmlSchema from a database and need to find the nodes that represent the Table and Column names. I have included a sample Xml Schema and the code I used to find the information. As this is the first time I have even attempted to use XPath, I was wondering if the code I provided is appropriate or if there is a better way of doing this. The <xs:element> tags are the nodes that I am looking for. As I am scraping a database, I am NOT guarenteed to always have the exact structure below. Any help is appreciated. Dave =====================================================...

Deserialize an XmlNode, HOW?
Lets say I have an XmlNode created by: Dim cs As XmlSerializer = New XmlSerializer(GetType(String)) Dim cms As MemoryStream = New MemoryStream cs.Serialize(cms, "This is a test") cms.Position = 0 Dim cd As XmlDocument = New XmlDocument cd.Load(cms) Dim contents As XmlNode = ImportNode(cd.DocumentElement, True) how to I deserialize this node again...I've tried something like: Dim cs As XmlSerializer = New XmlSerializer(GetType(String)) Dim ms As MemoryStream = New MemoryStream Dim xw As XmlWriter = New XmlTextWriter(ms, Nothing) c.WriteContentTo(xw) xw.Flush() ms.Position = ...

XMLnodelist and XPATH Problem
I am having a problem selecting nodes using the XMLnodelist Selectnodes using XPATH when I use XML SPY is successfully queries but when is use VB.net it comes up with nothing. Here is my code Dim nodess As XmlNode nodess = myNode.SelectSingleNode("//Web[@Url= 'http://localhost']") If nodess Is Nothing Then SQLrw.Delete() End If nodess = Nothing The myNode Properites ------------------------------------------------------------------------------------------------ {System.Xml.XmlElement} [System.Xm...

What is the Xpath
Hello, Thanks for the response Oleg. Now I need to know few answers. How do I get the value of a node using the xpath. I want the value of the MessageID and ResultCode from secondxml. And using this I want to delete the Child Nodes in the firstxml document. I am using the fowllowing code but the ni.current.value has values of all nodes in the file. I am confused. XmlDocument doc = new XmlDocument(); doc.Load("Firstxml.xml") hDocument doc2 = new XPathDocument("second.xml"); XPathNavigator nav = doc2.CreateNavigator(); foreach (XmlNo...