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 © 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
。我最近写了一个类似的程序代码,但它现在有一个命名空间,并不明白问题是什么。
尝试删除'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 +“地点”)。价值,这两者都不需要。 –