2013-06-26 33 views
0

我试图解析这个XML作为来自Virtual Earth的响应检索;Windows Phone分析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 © 2013 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> 
ebbe16390026429f967a06977c3bc94c|MIAM000025|02.00.150.1300|MIAMSNVM001367, EWRMSNVM003182 
</TraceId> 
<ResourceSets> 
<ResourceSet> 
<EstimatedTotal>1</EstimatedTotal> 
<Resources> 
<Location> 
<Name>TE-V 1009, 44167 Camañas (TE)</Name> 
<Point> 
<Latitude>40.640868544578552</Latitude> 
<Longitude>-1.1300414800643921</Longitude> 
</Point> 
<BoundingBox> 
<SouthLatitude>40.637005827007876</SouthLatitude> 
<WestLongitude>-1.1368284398869133</WestLongitude> 
<NorthLatitude>40.644731262149229</NorthLatitude> 
<EastLongitude>-1.1232545202418709</EastLongitude> 
</BoundingBox> 
<EntityType>Address</EntityType> 
<Address> 
<AddressLine>TE-V 1009</AddressLine> 
<AdminDistrict>Aragon</AdminDistrict> 
<AdminDistrict2>TE</AdminDistrict2> 
<CountryRegion>Spain</CountryRegion> 
<FormattedAddress>TE-V 1009, 44167 Camañas (TE)</FormattedAddress> 
<Locality>Camañas</Locality> 
<PostalCode>44167</PostalCode> 
</Address> 
<Confidence>Medium</Confidence> 
<MatchCode>Good</MatchCode> 
<GeocodePoint> 
<Latitude>40.640868544578552</Latitude> 
<Longitude>-1.1300414800643921</Longitude> 
<CalculationMethod>Interpolation</CalculationMethod> 
<UsageType>Display</UsageType> 
<UsageType>Route</UsageType> 
</GeocodePoint> 
</Location> 
</Resources> 
</ResourceSet> 
</ResourceSets> 
</Response> 

我找过任何答案,可能适用于我在这里或某处。但是每个人都以其他方式询问他们想要什么,而我什么都找不到。我只想获得由“Address”组成的“Locality”值作为字符串值。

我把这个XML命名为MyXMLString。

在此先感谢。

回答

1

你可以用XDocument.Parse

XDocument doc = XDocument.Parse(text); 
// Just as an example of using the namespace... 
XNamespace ns = "http://schemas.microsoft.com/search/local/ws/rest/v1"; 
foreach (var location in doc.Descendants(ns + "Address")) 
{ 
    Console.WriteLine(location.Element(ns + "Locality").Value); 
} 

如果Parse是不是在Windows Phone 7的使用,你可以使用一个StringReader

var reader = new StringReader(text); 
XDocument doc = XDocument.Load(reader); 

或者,如果不起作用:

var bytes = Encoding.UTF8.GetBytes(text); 
var stream = new MemoryStream(bytes); 
XDocument doc = XDocument.Load(stream); 
+0

Sure Parse is avail在WP7中能够胜任,但我认为我们在这条线上遇到了问题; location.Element(ns +“Name”)。Value。那是什么名字?我只想得到“”这是“

”的孩子。那么我应该如何编辑你的代码? – ilerleartik

+0

@ilerleartik:这是元素的名称。您应该阅读“Descendants”和“Element”方法的文档。我会更新我的示例给你'Locality'元素,但是当给定一个包含你不熟悉的方法的代码示例时,你确实需要做一些研究。 –

+0

另外,地点是由许多父母组成的叶子。它的父母是地址,父母的地址是位置,位置也有父母,所以......我应该在解析器中编写这个树关系吗? – ilerleartik