XPath usage for System.XML namespace objects

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Plater
    Recognized Expert Expert
    • Apr 2007
    • 7872

    XPath usage for System.XML namespace objects

    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:
    [code=xml]
    <?xml version="1.0" encoding="UTF-8" ?>

    <kml xmlns="http://earth.google.co m/kml/2.0">
    <Document>
    <Placemark>
    <name>Route</name>
    <description>
    <![CDATA[ Distance: 2,164&#160;mi (about 1 day 7 hours)<br/>Map data &#169;2009 Sanborn, Tele Atlas ]]>
    </description>
    </Placemark>
    </Document>
    </kml>
    [/code]

    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.SelectSingl eNode("/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["descriptio n"];

    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
    Recognized Expert Expert
    • Oct 2006
    • 9749

    #2
    Hey Plater, I moved this to the XML forum since the experts here know how to use XPath expressions.

    Comment

    • Plater
      Recognized Expert Expert
      • Apr 2007
      • 7872

      #3
      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.

      Comment

      • jkmyoung
        Recognized Expert Top Contributor
        • Mar 2006
        • 2057

        #4
        Namespace. xmlns="http://earth.google.co m/kml/2.0"
        Try looking at XMLNamespaceMan ager when using SelectNodes.


        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" );

        Comment

        • Plater
          Recognized Expert Expert
          • Apr 2007
          • 7872

          #5
          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.

          Comment

          • jkmyoung
            Recognized Expert Top Contributor
            • Mar 2006
            • 2057

            #6
            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"

            Comment

            Working...