1

In my application, I'm converting from one web service to another. I get an XML response as an XmlDocument. I'm trying to get specific nodes in the document. I know there will only ever be one instance of the node I'm looking for. The previous implementer was able to get exactly what he wants with:

XmlNode node = xmlDoc.SelectSingleNode("//result/geometry/location/lat/text()");

I'm trying to do the same with my response, but always get null back. I know (vaguely) what his XML response looked like, and know mine. But I can't use his syntax. I get null no matter what. I'm using a more complex statement:

XmlNode xmlNode = xmlDoc.GetElementsByTagName("StatusDescription").Item(0);

But, as you can see, it's ugly. And--worse--whenever I try to go more than one node deep, I get null back:

XmlNode xmlNode = xmlDoc.GetElementsByTagName("/ResourceSets/ResourceSet").Item(0);

I've tried inserting and removing slashes in several places, but to no avail. The XML the previous implementer got back isn't anything special; it's just XML. But he can jump all over the place with ease.

Here's a snippet of his XML response:

<GeocodeResponse>
 <status>OK</status>
 <result>
  <geometry>
   <location>
    <lat>37.4217550</lat>
    <lng>-122.0846330</lng>
   </location>
  </geometry>
 </result>
</GeocodeResponse>

Here's a snippet of mine:

<Response xmlns="http://schemas.microsoft.com/search/local/ws/rest/v1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <StatusCode>200</StatusCode>
  <StatusDescription>OK</StatusDescription>
  <ResourceSets>
    <ResourceSet>
      <EstimatedTotal>1</EstimatedTotal>
        <Resources>
          <Location>
              ...
          </Location>
        </Resources>
    </ResourceSet>
  </ResourceSets>
</Response>

Any idea how I can traverse the XML as easy as him?

Frecklefoot
  • 1,660
  • 2
  • 21
  • 52
  • Did you try the obvious `XmlNode node = xmlDoc.SelectSingleNode("//StatusDescription/text()");` and `XmlNode node = xmlDoc.SelectSingleNode("/ResourceSets/ResourceSet/");`? – Ken White Oct 16 '15 at 01:43

1 Answers1

2

The fundamental difference here is that his XML doesn't have default namespace, while yours has default namespace declared here :

xmlns="http://schemas.microsoft.com/search/local/ws/rest/v1"

By the way, this topic (XPath query against XML with default namespace) has been asked a lot previously in various forms here in SO. Notice that the element where a default namespace is declared and all of its descendants without a prefix and without a different default namespace declaration are considered to be in that aforementioned default namespace. And one possible way to query an element in a namespace is by mapping a prefix to the namespace uri using a namespace manager, and then using the mapped prefix properly in the XPath, for example :

var nsManager As New XmlNamespaceManager(new NameTable());
nsManager.AddNamespace("d", "http://schemas.microsoft.com/search/local/ws/rest/v1");
XmlNode xmlNode = xmlDoc.SelectSingleNode("//d:StatusDescription", nsManager);
har07
  • 88,338
  • 12
  • 84
  • 137
  • Thanks, har07. The did the trick! I did search for the answer first, but didn't find it. I guess I didn't know what precisely to search for. :/ – Frecklefoot Oct 16 '15 at 14:37