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!

XML document SelectNodes Q? 1

Status
Not open for further replies.

NoCoolHandle

Programmer
Apr 10, 2003
2,321
US
Ok..
My limited understanding
if I load an XML both of the following two methods should find the Node and return access to isn't nodes and values.

Code:
 dim xd as new XMLDocument()
 xd.Load(<i>pathToDoc</i>)
 dim nl as XMLNodeList = xd.SelectNodes("//LineString")
[green]' or [/green]
 xd.GetElementsByTagName("LineString")

However my XMLNodeList is always empty and the GetElementsByTag always returns multiple values.

What is wrong with the SelectNodes Syntax?

(The xml doc is a kml Document) fundamental node hirachy looks somehting like) - I need to get the coords, but only for LineString Items.

TIA Rob
Code:
<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="[URL unfurl="true"]http://earth.google.com/kml/2.2">[/URL]
<Document>
   <Folder>
     <styleUrl>
         <LineString>
              <coordinates>
                 LAT AND LONG...
              </coordinates>
         </LineString>
     </styleUrl>
   </Folder>
</Document>       
</kml>
 
The reason is that the xml document has in fact defined its vocabulary in an overlooked default namespace. Hence, to reference to its nodes etc, you have to take care of this fact by using XmlNamespaceManger
ref
The literal transcription of the sample there to this case is this.
[tt]
dim xd as new XMLDocument()
xd.Load(pathToDoc)
[blue]dim nsmgr as XmlNamespaceManager = new XmlNamespaceManager(xd.NameTable)
nsmgr.AddNamespace("[/blue][red]df[/red][blue]","[ignore][/ignore]")[/blue]
dim nl as XMLNodeList = xd.SelectNodes("//[red]df:[/red]LineString")
[/tt]
Note that "df" is quite arbitrary chosen, you can call it something else as long as its a kind of token of acceptable data type.
 
Thanks for the hint. It didn't work however.
specificly the error returned was


"Namespace Manager or XsltContext needed. This query has a prefix, variable, or user-defined function."

What a Pain.. The SelectNodes method seems to be almost broken by design. No namespace. No query.. Just about easier to do the whole thing using string.indexOf :)

Chasing the link to didn't seem to help.

Rob
 
Sorry, forgotten to edit fully this line.
[tt]
dim nl as XMLNodeList = xd.SelectNodes("//df:LineString", [red]nsmgr[/red])
[/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top