为什么我的方法总是返回 null?

发布于 2024-12-09 17:55:03 字数 3312 浏览 0 评论 0原文

<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">
  <Copyright>Copyright &#xA9; 2011 Microsoft and its suppliers. All rights
   reserved. This API cannot be accessed and the content and any results
   may not be used, reproduced or transmitted in any manner without express
   written permission from Microsoft Corporation.</Copyright>
  <BrandLogoUri>[http://dev.virtualearth.net/Branding/logo_powered_by.png]</BrandLogoUri>
  <StatusCode>200</StatusCode>
  <StatusDescription>OK</StatusDescription>
  <AuthenticationResultCode>ValidCredentials</AuthenticationResultCode>
  <TraceId>50230e70257e4ed5a5002a3d4a625c83|LTSM001156|02.00.159.1700|LTSMSNVM001471, LTSMSNVM001477</TraceId>
  <ResourceSets>
    <ResourceSet>
      <EstimatedTotal>1</EstimatedTotal>
      <Resources>
        <Location>
          <Name>1 Microsoft Way, Redmond, WA 98052</Name>
          <Point>
            <Latitude>47.640568390488625</Latitude>
            <Longitude>-122.1293731033802</Longitude>
          </Point>
          <BoundingBox>
            <SouthLatitude>47.636705672917948</SouthLatitude>
            <WestLongitude>-122.137016420622</WestLongitude>
            <NorthLatitude>47.6444311080593</NorthLatitude>
            <EastLongitude>-122.1217297861384</EastLongitude>
          </BoundingBox>
          <EntityType>Address</EntityType>
          <Address>
            <AddressLine>1 Microsoft Way</AddressLine>
            <AdminDistrict>WA</AdminDistrict>
            <AdminDistrict2>King Co.</AdminDistrict2>
            <CountryRegion>United States</CountryRegion>
            <FormattedAddress>1 Microsoft Way, Redmond, WA 98052</FormattedAddress>
            <Locality>Redmond</Locality>
            <PostalCode>98052</PostalCode>
          </Address>
          <Confidence>Medium</Confidence>
        </Location>
      </Resources>
    </ResourceSet>
  </ResourceSets>
</Response>

我的查询以前看起来像:

private void getData()
{
    // Api letőltése
    WebClient webClient = new WebClient();

    string url = "http://dev.virtualearth.net/REST/v1/Locations/" 
                 + _location + "?o=xml&key=App-asdf";

    webClient.DownloadStringCompleted += (s, e) =>
        {
            if (e.Error != null)
                return;

            XDocument xmlLocation = XDocument.Parse(e.Result);

            var ns = XNamespace.Get("http://schemas.microsoft.com/search/local/ws/rest/v1");
            var locality = from q in xmlLocation.Descendants(ns + "Address")
                           select (string)q.Element(ns + "Locality").Value;
        };

    webClient.DownloadStringAsync(new Uri(url));
}

为什么它总是返回 null

我想查询位置,但我的变量始终包含 null。我最近编写了一个类似的程序代码,该代码可以工作,但现在有一个名称空间,并且不明白问题是什么。

<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">
  <Copyright>Copyright © 2011 Microsoft and its suppliers. All rights
   reserved. This API cannot be accessed and the content and any results
   may not be used, reproduced or transmitted in any manner without express
   written permission from Microsoft Corporation.</Copyright>
  <BrandLogoUri>[http://dev.virtualearth.net/Branding/logo_powered_by.png]</BrandLogoUri>
  <StatusCode>200</StatusCode>
  <StatusDescription>OK</StatusDescription>
  <AuthenticationResultCode>ValidCredentials</AuthenticationResultCode>
  <TraceId>50230e70257e4ed5a5002a3d4a625c83|LTSM001156|02.00.159.1700|LTSMSNVM001471, LTSMSNVM001477</TraceId>
  <ResourceSets>
    <ResourceSet>
      <EstimatedTotal>1</EstimatedTotal>
      <Resources>
        <Location>
          <Name>1 Microsoft Way, Redmond, WA 98052</Name>
          <Point>
            <Latitude>47.640568390488625</Latitude>
            <Longitude>-122.1293731033802</Longitude>
          </Point>
          <BoundingBox>
            <SouthLatitude>47.636705672917948</SouthLatitude>
            <WestLongitude>-122.137016420622</WestLongitude>
            <NorthLatitude>47.6444311080593</NorthLatitude>
            <EastLongitude>-122.1217297861384</EastLongitude>
          </BoundingBox>
          <EntityType>Address</EntityType>
          <Address>
            <AddressLine>1 Microsoft Way</AddressLine>
            <AdminDistrict>WA</AdminDistrict>
            <AdminDistrict2>King Co.</AdminDistrict2>
            <CountryRegion>United States</CountryRegion>
            <FormattedAddress>1 Microsoft Way, Redmond, WA 98052</FormattedAddress>
            <Locality>Redmond</Locality>
            <PostalCode>98052</PostalCode>
          </Address>
          <Confidence>Medium</Confidence>
        </Location>
      </Resources>
    </ResourceSet>
  </ResourceSets>
</Response>

My query formerly looked like:

private void getData()
{
    // Api letőltése
    WebClient webClient = new WebClient();

    string url = "http://dev.virtualearth.net/REST/v1/Locations/" 
                 + _location + "?o=xml&key=App-asdf";

    webClient.DownloadStringCompleted += (s, e) =>
        {
            if (e.Error != null)
                return;

            XDocument xmlLocation = XDocument.Parse(e.Result);

            var ns = XNamespace.Get("http://schemas.microsoft.com/search/local/ws/rest/v1");
            var locality = from q in xmlLocation.Descendants(ns + "Address")
                           select (string)q.Element(ns + "Locality").Value;
        };

    webClient.DownloadStringAsync(new Uri(url));
}

Why does it always return null?

I want to query the locality but my variable always contains null. I recently wrote a similar program code which worked but now have a namespace and don't understand what the problem would be.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

人间☆小暴躁 2024-12-16 17:55:03

我刚刚针对列出的 XML 运行了您的查询,它生成了“Redmond”。

所以:把它分解。您遇到通信或线程问题。

    if (e.Error != null)
                return;

这是掩盖问题的好方法。

I just ran your query against the listed XML and it produces "Redmond".

So: Break it down. You have a communication or threading problem.

    if (e.Error != null)
                return;

Is a good way to sweep the problem under the rug.

無處可尋 2024-12-16 17:55:03
var locality = xmlLocation.Descendants(ns + "Locality").Select( x => x.Value);
var locality = xmlLocation.Descendants(ns + "Locality").Select( x => x.Value);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文