C# 如何查询地址?(LINQ到XML) 版权所有©2011微软及其供应商。版权所有。未经微软公司明确书面许可,不得访问本API,不得以任何方式使用、复制或传播本API的内容和任何结果。 [http://dev.virtualearth.net/Branding/logo_powered_by.png] 200 好啊 有效存款 50230e70257e4ed5a5002a3d4a625c83 | LTSM001156 | 02.00.159.1700 | LTSMSNVM001471,LTSMSNVM001477 1. 华盛顿州雷德蒙微软路1号,邮编:98052 47.640568390488625 -122.1293731033802 47.636705672917948 -122.137016420622 47.6444311080593 -122.1217297861384 地址 1微软之路 华盛顿州 金公司。 美国 华盛顿州雷德蒙微软路1号,邮编:98052 雷德蒙 98052 中等

C# 如何查询地址?(LINQ到XML) 版权所有©2011微软及其供应商。版权所有。未经微软公司明确书面许可,不得访问本API,不得以任何方式使用、复制或传播本API的内容和任何结果。 [http://dev.virtualearth.net/Branding/logo_powered_by.png] 200 好啊 有效存款 50230e70257e4ed5a5002a3d4a625c83 | LTSM001156 | 02.00.159.1700 | LTSMSNVM001471,LTSMSNVM001477 1. 华盛顿州雷德蒙微软路1号,邮编:98052 47.640568390488625 -122.1293731033802 47.636705672917948 -122.137016420622 47.6444311080593 -122.1217297861384 地址 1微软之路 华盛顿州 金公司。 美国 华盛顿州雷德蒙微软路1号,邮编:98052 雷德蒙 98052 中等,c#,wpf,silverlight,windows-phone-7,linq-to-xml,C#,Wpf,Silverlight,Windows Phone 7,Linq To Xml,如何从地址查询位置。我认为,本文是LINQ到XML对象。 我试过了 var asdf=来自xmlLocation.Elements(“地址”)中的q 选择q.Element(“Locality”).值; 但是不工作,为什么 如果你知道,请写信给我 谢谢Xml名称空间;由于数据不在默认命名空间中,因此需要: <Response xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/

如何从地址查询位置。我认为,本文是LINQ到XML对象。 我试过了 var asdf=来自xmlLocation.Elements(“地址”)中的q 选择q.Element(“Locality”).值; 但是不工作,为什么

如果你知道,请写信给我
谢谢

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">
<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>
然后在所有查询中使用它,即

XNamespace ns = "http://schemas.microsoft.com/search/local/ws/rest/v1";

代码的完整部分是XElement xmlLocation=XElement.Parse(e.Result);var ns=XNamespace.Get(“);var locality=from xmlLocation.subjections(ns+“Address”)选择q.Element(ns+“locality”).Value;@BalázsErdős事实上,如果xmlLocation是整个文档,那么您将需要子体—这一点并不清楚,因为xmlLocation从未定义过:p是结尾,我个人更喜欢(string)元素而不是元素.Value,因为它在很多情况下避免了null ref异常,并且与(int)元素(DateTime)很好地进行了移植元素等-使用LINQ to XML为您执行适当的解析(例如XML日期格式)我明白这一点,但我的查询没有返回任何信息。我有一个重要的家庭作业,但我还没有从中读到足够的内容。以前我写了一个LINQ到XML的程序代码,但这更容易。请你给我写一个好例子,我可以理解它。@BalázsErdős我可以稍后再看-我现在在手机上,不是id调试用eal(没有编译器!)@BalázsErdős仔细查看我的
ns
中的内容,实际上还有Adrian的
ns
——现在看看你给
ns
的内容。提示——这一切都很重要。修复它,它就会工作。
var asdf = from q in xmlLocation.Elements(ns + "Address")
           select (string) q.Element(ns + "Locality");
var ns = XNamespace.Get("http://schemas.microsoft.com/search/local/ws/rest/v1");
var asdf = from q in xmlLocation.Descendants(ns + "Address") 
           select q.Element(ns + "Locality").Value;