【发布时间】:2015-05-21 12:22:51
【问题描述】:
我想用 C# LINQ 阅读下面的 XMLfile。
我已经在 C# 中尝试过这段代码。但无法获取地址元素数据。
代码:
XNamespace ns = "http://www.w3.org/2001/XMLSchema";
XNamespace nsa = "http://www.w3.org/2001/XMLSchema-instance";
var Address = from r in XDocumentdata.Descendants(ns + "Address")
select new
{
Locality = r.Element(nsa + "Locality").Value,
CountryRegion = r.Element(nsa + "CountryRegion").Value
};
foreach (var r in Address)
{
string CountryRegion = r.CountryRegion;
string Locality = r.Locality;
}
XML:
<Response xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.microsoft.com/search/local/ws/rest/v1">
<Copyright>Copyright © 2015 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>1cf1896a29234bc583b75487b57e343f|HK20271655|02.00.163.1200|HK2SCH010280821, i-d219511f.ap-southeast-1b</TraceId>
<ResourceSets>
<ResourceSet>
<EstimatedTotal>5</EstimatedTotal>
<Resources>
<Location>
<Name>Panjagutta, Hyderabad 500082, India</Name>
<Point>
<Latitude>17.4176132</Latitude>
<Longitude>78.449595</Longitude>
</Point>
<BoundingBox>
<SouthLatitude>17.41759</SouthLatitude>
<WestLongitude>78.44907</WestLongitude>
<NorthLatitude>17.41764</NorthLatitude>
<EastLongitude>78.4502</EastLongitude>
</BoundingBox>
<EntityType>Address</EntityType>
<Address>
<AddressLine>Panjagutta</AddressLine>
<AdminDistrict>TS</AdminDistrict>
<AdminDistrict2>Hyderabad</AdminDistrict2>
<CountryRegion>India</CountryRegion>
<FormattedAddress>Panjagutta, Hyderabad 500082, India</FormattedAddress>
<Locality>Hyderabad</Locality>
<PostalCode>500082</PostalCode>
</Address>
<Confidence>Medium</Confidence>
<MatchCode>Good</MatchCode>
<GeocodePoint>
<Latitude>17.4176132</Latitude>
<Longitude>78.449595</Longitude>
<CalculationMethod>Interpolation</CalculationMethod>
<UsageType>Display</UsageType>
<UsageType>Route</UsageType>
</GeocodePoint>
</Location>
</Resources>
</ResourceSet>
</ResourceSets>
</Response>
【问题讨论】:
标签: c# xml linq web-services