【问题标题】:Why does my method always return null?为什么我的方法总是返回 null?
【发布时间】:2011-12-08 07:24:31
【问题描述】:
<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。我最近写了一个类似的程序代码,它可以工作,但现在有一个命名空间,不明白问题出在哪里。

【问题讨论】:

  • 尝试删除ns + 。它会起作用吗?
  • 作为 Henk,我用您发布的查询尝试了您发布的 XML,它找到了数据。因此,您发布的示例与您在运行代码时使用或获得的示例之间一定存在差异。一个可能的问题可能是命名空间的更改,您可以通过执行XNamespace ns = xmlLocation.Root.Name.Namespace; 而不是像当前那样对 URL 进行硬编码来避免这种情况。另请注意,select (string)q.Element(ns + "Locality").Value 应缩短为select (string)q.Element(ns + "Locality")select q.Element(ns + "Locality").Value,两者都不需要。

标签: c# xml-parsing linq-to-xml webclient


【解决方案1】:
var locality = xmlLocation.Descendants(ns + "Locality").Select( x => x.Value);

【讨论】:

    【解决方案2】:

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

    所以:分解它。您有通信或线程问题。

        if (e.Error != null)
                    return;
    

    是一个扫除问题的好方法。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-12
      • 2019-09-18
      • 2014-09-02
      • 2015-02-27
      • 2021-01-10
      • 1970-01-01
      相关资源
      最近更新 更多