0
我打電話Bing地圖服務返回一些XML這裏獲取緯度/經度:使用LINQ to從Bing地圖REST服務
http://dev.virtualearth.net/REST/v1/Locations/Encoded_Address?o=xml&key=Maps_Key
對於我的生活,我似乎無法到使用Linq獲取經度和緯度!它總是會返回null
。下面是我使用的(我用亞特蘭大爲例)代碼:
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>
我試過了。沒有骰子。 XML文件上有三個命名空間......一些使用XSI和XSD模式......做那些事情嗎? – Armstrongest 2011-06-13 21:15:58
@Atømix我試過** [this](http://pastie.org/2063114)**,它打印出Lat和long。 – 2011-06-13 21:20:40
ARGH!你是對的。我認爲我的XML很混亂。我再次複製XML並且它工作。可能是嵌套的引號。謝謝一堆! – Armstrongest 2011-06-13 21:28:37