Goal: I am trying to retrieve a city name from an xml file
I am using an api that can give me all the information I need by indicating the latitude and longtitude.
This is an URL to the full XML that I am using: http://dev.virtualearth.net/REST/v1/Locations/50,50?o=xml&key=Avu1gnmc6hy50Jsb-l3U_iTbKyOXI2wnsVS1tj7UMtwJxesB9WDZs_qyG0zKgpkZ
And here is an excerpt from the XML that is returned, showing the relevant parts:
<?xml version="1.0" encoding="UTF-8"?>
<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">
...
<ResourceSets>
<ResourceSet>
<EstimatedTotal>1</EstimatedTotal>
<Resources>
<Location>
<Name>Kaztalovskiy rayon, Kazakhstan</Name>
...
<Address>
<AdminDistrict>Batys Qazaqstan</AdminDistrict>
<CountryRegion>Kazakhstan</CountryRegion>
<FormattedAddress>Kaztalovskiy rayon, Kazakhstan</FormattedAddress>
<Locality>Kaztalovskiy rayon</Locality>
</Address>
...
</Location>
</Resources>
</ResourceSet>
</ResourceSets>
</Response>
This is what I have tried:
HttpClient Client = new HttpClient();
string Result = await Client.GetStringAsync("http://dev.virtualearth.net/REST/v1/Locations/ " + position.Coordinate.Latitude +", " + position.Coordinate.Longitude +"?o=xml&key=Avu1gnmc6hy50Jsb-l3U_iTbKyOXI2wnsVS1tj7UMtwJxesB9WDZs_qyG0zKgpkZ");
XDocument ResultDocument = XDocument.Parse(Result);
XElement AddressElement = ResultDocument.Root.Element("ResourceSets");
string City = AddressElement.Element("Locality").Value;
I need to get information that's inside the big <ResourceSets>
block. Inside it has another block called <Address>
.
I am not sure how to get the contents of the <Locality>
field from it, I know that it is in the big block of <ResourceSets>
but how can I go deeper in it and tell the reader to get me that specific field?