How to grab location information in the XML using linq?
My xml looks like:
<kml xmlns="http://earth.google.com/kml/2.0">
<Response>
<name>90210</name>
<Status>
<code>200</code>
<request>geocode</request>
</Status>
<Placemark id="p1">
<address>Beverly Hills, CA 90210, USA</address>
<AddressDetails Accuracy="5"
xmlns="urn:oasis:names:tc:ciq:xsdschema:xAL:2.0">
<Country>
<CountryNameCode>US</CountryNameCode>
<CountryN开发者_运维问答ame>USA</CountryName>
<AdministrativeArea>
<AdministrativeAreaName>CA</AdministrativeAreaName>
<SubAdministrativeArea>
<SubAdministrativeAreaName>Los
Angeles</SubAdministrativeAreaName>
<Locality>
<LocalityName>Beverly Hills</LocalityName>
<PostalCode>
<PostalCodeNumber>90210</PostalCodeNumber>
</PostalCode>
</Locality>
</SubAdministrativeArea>
</AdministrativeArea></Country>
</AddressDetails>
<ExtendedData>
<LatLonBox north="34.1377559" south="34.0642330"
east="-118.3896720" west="-118.4467160" />
</ExtendedData>
<Point>
<coordinates>-118.4104684,34.1030032,0</coordinates>
</Point>
</Placemark>
</Response>
</kml>
I need the information in extended data i.e. values for north/south, east/west.
You can also drill down though all of the nodes. But this is probably the simplest way to get the LatLonBox
element.
var xml = XElement.Parse(xmlString);
var ns = "{http://earth.google.com/kml/2.0}";
var extendedData = xml.Descendants(ns + "LatLonBox").First();
var locationBox = new
{
North = float.Parse(extendedData.Attribute("north").Value),
South = float.Parse(extendedData.Attribute("south").Value),
East = float.Parse(extendedData.Attribute("east").Value),
West = float.Parse(extendedData.Attribute("west").Value),
};
... to drill down the elements you can do this ...
var extendedData = xml.Element(ns + "Response")
.Element(ns + "Placemark")
.Element(ns + "ExtendedData")
.Element(ns + "LatLonBox");
精彩评论