2011-10-15 237 views
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> 

我以前的查詢看起來像:爲什麼我的方法總是返回null?

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。我最近寫了一個類似的程序代碼,但它現在有一個命名空間,並不明白問題是什麼。

+0

嘗試刪除'ns +'。它會起作用嗎? –

+0

正如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 +「地點」)。價值,這兩者都不需要。 –

回答

1

我剛剛對列出的XML運行了查詢,並生成「Redmond」。

所以:打破它。你有通信或線程問題。

if (e.Error != null) 
       return; 

是一個很好的方式來掃描地毯下的問題。

0
var locality = xmlLocation.Descendants(ns + "Locality").Select(x => x.Value); 
相關問題