473,396 Members | 2,002 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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 4276
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

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

Similar topics

3
by: Ian Taite | last post by:
Hello, I'm exploring why one of my C# .NET apps has "high" memory usage, and whether I can reduce the memory usage. I have an app that wakes up and processes text files into a database...
1
by: alpha | last post by:
Hi Friends, When we need the xpath namespace as we can traverse the xml document using xmldocument object like using selectSingleNode or selectNodes methods. In system.xml.xpath namespace we have to...
1
by: Sonu Kapoor | last post by:
Hi, I would like to use xpath with xslt. I know that this should be possible, but I dont get it ! Here is what I have tried so far: ==================================== my xml file:...
1
by: Robert | last post by:
I am having a problem selecting nodes using the XMLnodelist Selectnodes using XPATH when I use XML SPY is successfully queries but when is use VB.net it comes up with nothing. Here is my code ...
10
by: Michael C# | last post by:
OK, here's the deal. I have a small XML file that represents a small database table. I load it into a System.XML.XMLDocument. So far so good. I run an XPath query against it to retrieve all the...
18
by: jacksu | last post by:
I have a simple program to run xpath with xerces 1_2_7 XPathFactory factory = XPathFactory.newInstance(); XPath xPath = factory.newXPath(); XPathExpression xp = xPath.compile(strXpr);...
9
by: David Thielen | last post by:
Hi; I am sure I am missing something here but I cannot figure it out. Below I have a program and I cannot figure out why the xpath selects that throw an exception fail. From what I know they...
3
by: Jason Mobarak | last post by:
Hello -- I'm attempting to get a handle on how to do xpath queries with System.Xml -- so far the biggest hurdle has been how to deal with a default namespace. If I use the test xml: <?xml...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.