Использование Linq для получения Lat / Lon от службы REST Bing Maps - PullRequest
0 голосов
/ 14 июня 2011

Я звоню в службу карт Bing, чтобы вернуть сюда XML:

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

Для жизни я не могу получить долготу и широту с помощью Linq! Всегда возвращает null. Вот код, который я использую (я использовал Атланту, GA в качестве примера):

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();
}

Я также попытался добавить пространство имен:

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

Но НЕТ. Что я делаю не так ???

Вот соответствующий фрагмент XML (здесь только соответствующие части)

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

1 Ответ

1 голос
/ 14 июня 2011

Вам нужно будет включить пространство имен во всех местах, как показано ниже.

// 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();
}
...