views:

47

answers:

1

My xml looks like:

<kml xmlns="http://earth.google.com/kml/2.0"&gt;
  <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>
          <CountryName>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.

+3  A: 

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");
Matthew Whited
The xml namespaces can be rather messy. Using the `ns +` like I did above dramatically simplifies the mess.
Matthew Whited
thanks allot, so you are also using an anonymous class there?
Blankman
for `locationBox` yes. you could replace that with a class definition or split it into 4 variables if you want. If you were trying to process a set of elements you could also put that into your LINQ statement.
Matthew Whited
I should note that there are plenty of places in this code can could throw null reference exceptions. But if you are using this against a known schema you can probably ignore the possible Null values. Each of the chained `.Element(...)` methods and the `.Value` after the `.Attribute(...)` could each throw a null ref.
Matthew Whited