Selecting data using XPathNodeIterator - Namespace syntax

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
Id like to display the contents of an XML file in a data list and need some help with namespaces in the Xpath expression. I imagine this should be a simple question for someone on this forum!
Heres an example without a namespace that works:
<div style="color:Black;background-color:White; <pre>
<span style="color:Blue; <?<span style="color:#A31515; xml <span style="color:Red; version<span style="color:Blue; =<span style="color:Black; "<span style="color:Blue; 1.0<span style="color:Black; " <span style="color:Red; encoding<span style="color:Blue; =<span style="color:Black; "<span style="color:Blue; utf-8<span style="color:Black; " <span style="color:Blue; ?>
<span style="color:Blue; <<span style="color:#A31515; RootElement<span style="color:Blue; >
<span style="color:Blue; <<span style="color:#A31515; Screen <span style="color:Red; Name<span style="color:Blue; =<span style="color:Black; "<span style="color:Blue; Test1<span style="color:Black; "<span style="color:Blue; >
<span style="color:Blue; </<span style="color:#A31515; Screen<span style="color:Blue; >
<span style="color:Blue; <<span style="color:#A31515; Screen <span style="color:Red; Name<span style="color:Blue; =<span style="color:Black; "<span style="color:Blue; Test2<span style="color:Black; "<span style="color:Blue; >
<span style="color:Blue; </<span style="color:#A31515; Screen<span style="color:Blue; >
<span style="color:Blue; </<span style="color:#A31515; RootElement <span style="color:Blue; >
[/code]
<div style="color:Black;background-color:White; <pre>
<span style="color:Blue; Dim xpathDoc <span style="color:Blue; As XPathDocument
<span style="color:Blue; Dim xmlNav <span style="color:Blue; As XPathNavigator
<span style="color:Blue; Dim xmlNI <span style="color:Blue; As XPathNodeIterator
xpathDoc = <span style="color:Blue; New XPathDocument(txlFilePath.Text)
xmlNav = xpathDoc.CreateNavigator()
xmlNI = xmlNav.<span style="color:Blue; Select(<span style="color:#A31515; "/RootElement/Screen")

<span style="color:Blue; While (xmlNI.MoveNext())
lstScreens.Items.Add(xmlNI.Current.GetAttribute(<span style="color:#A31515; "Name", <span style="color:#A31515; ""))
<span style="color:Blue; End <span style="color:Blue; While
[/code]
Can someone show me the correct syntax if a namespace is added at the RootElement level? Heres my attempt which doesnt work and returns 0 elements.
<div style="color:Black;background-color:White; <pre>
<span style="color:Blue; <?<span style="color:#A31515; xml <span style="color:Red; version<span style="color:Blue; =<span style="color:Black; "<span style="color:Blue; 1.0<span style="color:Black; " <span style="color:Red; encoding<span style="color:Blue; =<span style="color:Black; "<span style="color:Blue; utf-8<span style="color:Black; " <span style="color:Blue; ?>
<span style="color:Blue; <<span style="color:#A31515; RootElement <span style="color:Red; xmlns<span style="color:Blue; =<span style="color:Black; "<span style="color:Blue; http://tempuri/tim<span style="color:Black; "<span style="color:Blue; >
<span style="color:Blue; <<span style="color:#A31515; Screen <span style="color:Red; Name<span style="color:Blue; =<span style="color:Black; "<span style="color:Blue; Test1<span style="color:Black; "<span style="color:Blue; >
<span style="color:Blue; </<span style="color:#A31515; Screen<span style="color:Blue; >
<span style="color:Blue; <<span style="color:#A31515; Screen <span style="color:Red; Name<span style="color:Blue; =<span style="color:Black; "<span style="color:Blue; Test2<span style="color:Black; "<span style="color:Blue; >
<span style="color:Blue; </<span style="color:#A31515; Screen<span style="color:Blue; >
<span style="color:Blue; </<span style="color:#A31515; RootElement <span style="color:Blue; >
[/code]
<div style="color:Black;background-color:White; <pre>
<span style="color:Blue; Dim xpathDoc <span style="color:Blue; As XPathDocument
<span style="color:Blue; Dim xmlNav <span style="color:Blue; As XPathNavigator
<span style="color:Blue; Dim xmlNI <span style="color:Blue; As XPathNodeIterator
xpathDoc = <span style="color:Blue; New XPathDocument(txlFilePath.Text)
xmlNav = xpathDoc.CreateNavigator()


<span style="color:Green; Is this correct?
<span style="color:Blue; Dim nsMan <span style="color:Blue; As XmlNamespaceManager = <span style="color:Blue; New XmlNamespaceManager(xmlNav.NameTable)

<span style="color:Green; What syntax can I use to pick up http://tempuri/tim from the xpathDoc?
nsMan.AddNamespace(<span style="color:#A31515; "y", <span style="color:#A31515; "http://tempuri/tim")

<span style="color:Green; Whats the correct syntax to use here? It currently returns 0 elements
xmlNI = xmlNav.<span style="color:Blue; Select(<span style="color:#A31515; "/y:RootElement/Screen", nsMan)

<span style="color:Blue; While (xmlNI.MoveNext())
lstScreens.Items.Add(xmlNI.Current.GetAttribute(<span style="color:#A31515; "Name", <span style="color:#A31515; ""))
<span style="color:Blue; End <span style="color:Blue; While
[/code]
The second question is what syntax do I need to pick up the namespace value ( http://tempuri/tim http://tempuri/tim ) from the xpathDoc? At the moment, Im hardcoding this but would prefer to pick this up from the file.<br/>
<br/>
Thanks in advance,<br/>
Tim

View the full article
 
Back
Top