0

I'm calling the Bing Maps Service to return some XML here:

http://dev.virtualearth.net/REST/v1/Locations/Encoded_Address?o=xml&key=Maps_Key

For the life of me, I can't seem to get the Longitude and Latitude using Linq! It always returns null. Here's the code I'm using (I've used Atlanta, GA as an example):

xml = XElement.Load(url); // url is as above
var locations = from l in xml.Descendants( "Location" )
                        select l;
// Output to Test    
foreach(var location in xml.Descendants("Location")){
  // We NEVER get in here.
  Console.WriteLine( "Lat: "  + location.Descendants("Latitude").First().Value );
  Console.WriteLine( "Long: " + location.Descendants("Longitude").First().Value);
  Console.ReadLine();
}

I've also tried adding a namespace:

XNamespace xn = "http://schemas.microsoft.com/search/local/ws/rest/v1";
xml.Descendants(xn + "Location"))

But NO go. What am I doing wrong???

Here's the relevant XML fragment (only relevant parts are here)

<Response xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xmlns:xsd="http://www.w3.org/2001/XMLSchema"
          xmlns="http://schemas.microsoft.com/search/local/ws/rest/v1">
  <ResourceSets>
    <ResourceSet>
      <EstimatedTotal>1</EstimatedTotal>
      <Resources>
        <Location>
          <Name>Atlanta, GA</Name>
          <Point>
            <Latitude>33.748315</Latitude>
            <Longitude>-84.39111</Longitude>
          </Point>
          <!-- other stuff here -->
        </Location>
      </Resources>
    </ResourceSet>
  </ResourceSets>
</Response>
Armstrongest
  • 15,181
  • 13
  • 67
  • 106

1 Answers1

1

You'll have to include namespace in all places as shown below.

// Output to Test    
foreach(var location in xml.Descendants(xn +"Location")){
  // NOW WE GET IN HERE.
  Console.WriteLine( "Lat: "  + location.Descendants(xn +"Latitude").First().Value );
  Console.WriteLine( "Long: " + location.Descendants(xn + "Longitude").First().Value);
  Console.ReadLine();
}
Bala R
  • 107,317
  • 23
  • 199
  • 210
  • I've tried that. No dice. There are three namespaces on the XML file... some with XSI and XSD schemas... Do those matter? – Armstrongest Jun 13 '11 at 21:15
  • 2
    @Atømix I tried **[this](http://pastie.org/2063114)** and it prints Lat and long. – Bala R Jun 13 '11 at 21:20
  • ARGH! You're right. I think my XML was messed up. I copied the XML again and it worked. Probably the nested quotes. Thanks a bunch! – Armstrongest Jun 13 '11 at 21:28