0

I'm trying to parse this XML retrieved as a response from Virtual Earth;

<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>

I've looked for any answer that might work for me on here, or somewhere. But everyone asked in other ways for what they want, and I could not find anything. I just want to get "Locality" value consisted by "Address" as a string value.

I have this XML as a string Named MyXMLString.

Thanks in advance.

1 Answer 1

1

You can just use 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);
}

If Parse isn't available in Windows Phone 7, you could use a StringReader:

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

Or if that doesn't work:

var bytes = Encoding.UTF8.GetBytes(text);
var stream = new MemoryStream(bytes);
XDocument doc = XDocument.Load(stream);
Sign up to request clarification or add additional context in comments.

4 Comments

Sure Parse is available in WP7, but I think we are having a problem on this line; location.Element(ns + "Name").Value. What is that "Name"? I just want to get "<Locality >" which is a child of "<address>". So how should I edit your code?
@ilerleartik: It's the name of the element. You should read the documentation for the Descendants and Element methods. I'll update my sample to give you the Locality element, but you really need to do some research when given a code sample which contains methods you're unfamiliar with.
Additionaly, Locality is a leaf consisting lots of parents. Its parent is address, and the parent of address is Location, and location has also a parent, so on... Should I write this tree relation in my parser?
Sure you are right, I'm trying to do something but I'm really overloaded of researching how to do that, and my brain has gone :) , thanks for your helps. It worked like a charm, now this is time to test and learn how parsing works. Thanks again!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.