使用 Linq 从 Bing 地图 REST 服务获取纬度/经度
我正在调用 Bing 地图服务以在此处返回一些 XML:
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>
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>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您必须在所有位置包含名称空间,如下所示。
You'll have to include namespace in all places as shown below.