Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Navigating XML using XPath

Status
Not open for further replies.

Boknaai

Programmer
Dec 8, 2002
3
ZA
Hi All

I'm having a problem navigating through an XML document using XPath. I've never done this before, so I'm sure the answer is fairly obvious :)

I am using the following code to navigate to a particular node in an xml document (VB.NET):

Dim doc As XmlDocument = New XmlDocument()
doc.Load("doc.xml")

Dim nd As XmlNode
Dim rt As XmlElement = doc.DocumentElement
nd = rt.SelectSingleNode("descendant::page[@PageName='default.aspx']")

'output text to the page, to check it is working
Label1.Text = nd.OuterXml()

This works perfectly, as long as there is no XSD document associated with my xml document. eg If I have the following lines at the top of my xml document, it doesn't work:

<?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?>
<PageList xmlns=&quot;....

However, if I have:

<?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?>
<PageList>
...

It does work!!

Please could somebody tell me why this is so and what I should be doing instead.

Thanks
 
The attribute becomes a descendant node of the documentElement in the DOM document.

What you want to do is use the following:

Code:
nd = rt.SelectSingleNode(&quot;//Page[@PageName='default.aspx']&quot;)

If you want a direct descendant then you could use /rootnodename/Page[@PageName='default.aspx']

James :) James Culshaw
jculshaw@miniaturereview.co.uk
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top