Reputation: 101
I'm trying to read a KML file in a C #. I need to read this information to get the information and treat it. Is there a library to read a KML giving back the information in a data structure? If not, how do you read a KML file? Is it like reading a XML file?
Upvotes: 4
Views: 8652
Reputation: 775
Here is my way to parsing a KML file to get coordinates of object:
string elementToFind = "example";
System.IO.Stream kmlFile = System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream("objects.kml");
Xml.Linq.XDocument xDoc = System.Xml.Linq.XDocument.Load(kmlFile);
string xNs = "{" + xDoc.Root.Name.Namespace.ToString() + "}";
var coordsStr =
(from f in xDoc.Descendants(xNs + "Placemark")
where elementToFind.Contains(f.Parent.Element(xNs + "name").Value + f.Element(xNs + "name").Value)
select f.Element(xNs + "LineString").Element(xNs + "coordinates")).FirstOrDefault();
Parse it like a regular XML file a search for data you need. I hope this helped you a little.
Upvotes: 3
Reputation: 101
I've found the solution. The problem was the new xsd file have multiple namespace, so to convert to classes using XSD.exe I needed two more files: atom-author-link.xsd
and xal.xsd
After that I've used XSD.exe with this command line: xsd.exe /c ogckml22.xsd atom-author-link.xsd xal.xsd
Finally, I had classes from xsd file.
Additional information:
I've just found this library (http://sharpkml.codeplex.com/) to read/write kml 2.2 files. It's an update of libkml
Upvotes: 3
Reputation: 5234
Google provides an XSD files with the schema for the KML files, you can find it at the location below.
http://code.google.com/apis/kml/schema/kml22gx.xsd
Here is the documentation for the KML format:
http://code.google.com/apis/kml/documentation/kmlreference.html
You could use the Microsoft XSD tool to generate a class from the schema:
http://msdn.microsoft.com/en-us/library/x6c1kb0s(v=VS.100).aspx
Upvotes: 0