473,382 Members | 1,743 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,382 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 4275
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.