使用 Linq 从 Bing 地图 REST 服务获取纬度/经度

发布于 2024-11-15 09:29:49 字数 1742 浏览 0 评论 0原文

我正在调用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

怪异←思 2024-11-22 09:29:49

您必须在所有位置包含名称空间,如下所示。

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

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();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文