I’m just starting off using linq queries within c#, and i’m struggling to get all the data i need.
Basically i’m using the google earth kml file as my xml file.
The structure looks like this.
<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://earth.google.com/kml/2.2">
<Document>
<Placemark>
<name>XXX</name>
<description>XXX</description>
<styleUrl>XXX</styleUrl>
<Point>
<coordinates>XXX</coordinates>
</Point>
</Placemark>
</Document>
</kml>
With my code as it is, i can get the first level of elements, (name, description, styleurl), but can’t get my syntax right in order to get the coordinates element within Point. Can anyone point me in the right direction? The line i’m struggling with is Coord = p.Element(ns + “Point”).Element(ns + “coordinates”).Value, what should this be?
XNamespace ns = "http://earth.google.com/kml/2.2";
var placemarks = xdoc.Descendants(ns + "Placemark")
.Select(p => new
{
Name = p.Element(ns + "name").Value,
Desc = p.Element(ns + "description").Value,
Coord = p.Element(ns + "Point")
.Element(ns + "coordinates").Value
}).ToList();
1 Answer