Reading in XML / KML files using C #

I am trying to import the Google earth kml xml file into the application, but I cannot get the xDocument syntax to do what I want to, I wonder if anyone can suggest a way to read the kml xml file.

I understand the basics of XML import, but I can’t get anything while working with xDocument and Linq, ideally I would like to get each Placemark as an object and add them to my db-driven Entity Framework. Any suggestions on how I should do this would be great, as I am just starting out with Linq and can do with some pointers. Xml laid out below

<?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> <Placemark> <name>XXX</name> <description>XXX</description> <styleUrl>XXX</styleUrl> <Point> <coordinates>XXX</coordinates> </Point> </Placemark> </Document> </kml> 
+8
c # xml import linq entity-framework
source share
4 answers

You did not specify any code, but I would assume that you forgot to include your namespace when referring to things. Here is an example.

Primary Access:

 var placemarks = xdoc.Element("kml").Element("Document").Elements("Placemark"); 

Using namespaces:

 var ns = XNamespace.Get("http://earth.google.com/kml/2.2"); var placemarks = xdoc.Element(ns + "kml").Element(ns + "Document").Elements(ns + "Placemark"); 
+7
source share

I assume that you forgot to use the namespace in your LINQ to XML queries. It is easy enough to extract data from this:

 XNamespace ns = "http://earth.google.com/kml/2.2"; var doc = XDocument.Load("file.xml"); var query = doc.Root .Element(ns + "Document") .Elements(ns + "Placemark") .Select(x => new PlaceMark // I assume you've already got this { Name = x.Element(ns + "name").Value, Description = x.Element(ns + "description").Value, // etc }); 

If this does not help, write a complete example of what you tried and what went wrong.

+5
source share

I used SharmpKml and documentation to extract information from a KML file.

 using SharpKml.Dom; using SharpKml.Engine; using SharpKml.Dom.GX; TextReader reader = File.OpenText(filePath); KmlFile file = KmlFile.Load(reader); _kml = file.Root as Kml; sPlaceMarks[] tempPlaceMarks = new sPlaceMarks[1000]; if (_kml != null) { foreach (var placemark in _kml.Flatten().OfType<Placemark>()) { tempPlaceMarks[numOfPlaceMarks].Name = placemark.Name; tempPlaceMarks[numOfPlaceMarks].Description = placemark.Description.Text; tempPlaceMarks[numOfPlaceMarks].StyleUrl = placemark.StyleUrl; tempPlaceMarks[numOfPlaceMarks].point = placemark.Geometry as SharpKml.Dom.Point; tempPlaceMarks[numOfPlaceMarks].CoordinateX = tempPlaceMarks[numOfPlaceMarks].point.Coordinate.Longitude; tempPlaceMarks[numOfPlaceMarks].CoordinateY = tempPlaceMarks[numOfPlaceMarks].point.Coordinate.Latitude; tempPlaceMarks[numOfPlaceMarks].CoordinateZ = tempPlaceMarks[numOfPlaceMarks].point.Coordinate.Altitude; numOfPlaceMarks++; } foreach (var lookAt in _kml.Flatten().OfType<LookAt>()) { Placemark placemark = (Placemark)lookAt.Parent; for (int i = 0; i < numOfPlaceMarks; i++) { if (placemark.Name == tempPlaceMarks[i].Name) { tempPlaceMarks[i].Name = placemark.Name; tempPlaceMarks[i].Description = placemark.Description.Text; tempPlaceMarks[i].StyleUrl = placemark.StyleUrl; tempPlaceMarks[i].altitude = lookAt.Altitude; tempPlaceMarks[i].AltitudeMode =(SharpKml.Dom.GX.AltitudeMode)lookAt.GXAltitudeMode; tempPlaceMarks[i].Heading = lookAt.Heading; tempPlaceMarks[i].Latitude = lookAt.Latitude; tempPlaceMarks[i].Longitude = lookAt.Longitude; tempPlaceMarks[i].Range = lookAt.Range; tempPlaceMarks[i].Tilt = lookAt.Tilt; break; } } } 
+3
source share
 var xDoc = XDocument.Load("a.xml"); 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 }) .ToList(); 
+2
source share

All Articles