This is the first in a series looking at how to work with KML files, the file format used by Google Earth. KML is an XML format and, rather than writing lots of XPath queries, it would be nice to generate classes that can be serialised to KML. This way, we get intellisense, and compile-time checking of our code. This part looks at generating classes from the KML schema, to make our lives much easier going forwards.
We will be using the XML Schema Definition Tool (xsd.exe) to generate the .NET classes. We will be outputting in C#, but most languages are supported through the /language option.
Firstly, download the schema itself, and save it to your local disk. I’m saving it to C:\source\kml.xsd.
Then, open up a command prompt and run the following command:
xsd.exe c:\source\kml.xsd /classes /language:cs /out:c:\source
This will generate a “kml.cs” file containing classes representing the schema, ready to add to the project we will be starting soon.