469,323 Members | 1,575 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,323 developers. It's quick & easy.

XPath usage for System.XML namespace objects

Plater
7,872 Expert 4TB
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)?
Jan 7 '09 #1
5 4169
Frinavale
9,735 Expert Mod 8TB
Hey Plater, I moved this to the XML forum since the experts here know how to use XPath expressions.
Jan 7 '09 #2
Plater
7,872 Expert 4TB
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.
Jan 7 '09 #3
jkmyoung
2,057 Expert 2GB
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");
Jan 7 '09 #4
Plater
7,872 Expert 4TB
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.
Jan 7 '09 #5
jkmyoung
2,057 Expert 2GB
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"
Jan 7 '09 #6

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

1 post views Thread by alpha | last post: by
1 post views Thread by Sonu Kapoor | last post: by
1 post views Thread by Robert | last post: by
10 posts views Thread by Michael C# | last post: by
9 posts views Thread by David Thielen | last post: by
3 posts views Thread by Jason Mobarak | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by mdpf | last post: by
reply views Thread by harlem98 | last post: by
reply views Thread by listenups61195 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.