Connecting Tech Pros Worldwide Forums | Help | Site Map

XPath usage for System.XML namespace objects

Plater's Avatar
Moderator
 
Join Date: Apr 2007
Location: New England
Posts: 7,161
#1: Jan 7 '09
I'm having a bit of trouble getting XPath expressions to work to select nodes.

I am working with the google maps results.
A shortened example xml:
Expand|Select|Wrap|Line Numbers
  1. <?xml version="1.0" encoding="UTF-8" ?> 
  2.  
  3. <kml xmlns="http://earth.google.com/kml/2.0">
  4.     <Document>
  5.         <Placemark>
  6.             <name>Route</name> 
  7.             <description>
  8.                 <![CDATA[ Distance: 2,164 mi (about 1 day 7 hours)<br/>Map data ©2009 Sanborn, Tele Atlas  ]]> 
  9.           </description>
  10.       </Placemark>
  11.   </Document>
  12. </kml>
  13.  
Now really there is a lot more to that xml, but what I am concerned with is the <Placemark> node that has the name of "Route", and then I want the contents of the description node.

After populating an XmlDocument doc, I tried to use this:
doc.SelectSingleNode("/kml/Document/placemark[name='route']/description");
and
doc.SelectNodes("/kml/Document/placemark[name='route']/description");

They always turn up no results.
I even tried:
doc.SelectNodes("//description");
which also produced no results.

I was able to get it by using a non-xpath expression:
doc.ChildNodes[1].ChildNodes[0].LastChild["description"];

But seems very unstable as it relies on those indexes existing.

Does anyone know what is wrong with the XPath I used? Is it a namespace issue (I set up no namespaces)?

Frinavale's Avatar
Site Moderator
 
Join Date: Oct 2006
Location: The Great White North
Posts: 5,137
#2: Jan 7 '09

re: XPath usage for System.XML namespace objects


Hey Plater, I moved this to the XML forum since the experts here know how to use XPath expressions.
Plater's Avatar
Moderator
 
Join Date: Apr 2007
Location: New England
Posts: 7,161
#3: Jan 7 '09

re: XPath usage for System.XML namespace objects


I can use XPath in other systems (like in FF's javascript) just fine, everything works as expected. The problem was with how .NET uses it.
I'll leave it here for now.
Moderator
 
Join Date: Mar 2006
Posts: 1,103
#4: Jan 7 '09

re: XPath usage for System.XML namespace objects


Namespace. xmlns="http://earth.google.com/kml/2.0"
Try looking at XMLNamespaceManager when using SelectNodes.
http://msdn.microsoft.com/en-us/library/4bektfx9.aspx

Don't forget you'll have to prefix each node. So, assuming ge is the namespace,
doc.SelectNodes("/ge:kml/ge:Document/ge:placemark[ge:name='route']/ge:description");
Plater's Avatar
Moderator
 
Join Date: Apr 2007
Location: New England
Posts: 7,161
#5: Jan 7 '09

re: XPath usage for System.XML namespace objects


Drat I was hoping to not have to deal with that.
I never should have left string functions.
hehe everytime I try to use XML to perform a task, I bail on it and decide its faster to just string function.
Moderator
 
Join Date: Mar 2006
Posts: 1,103
#6: Jan 7 '09

re: XPath usage for System.XML namespace objects


True, String functions would be faster if you don't have any other description nodes. Also, I forgot you'd probably have to change case, eg "route" != "Route"
Reply