This project has moved and is read-only. For the latest updates, please go here.
The library can be used to load a Kml file in memory and then iterate over its elements. To do this we need to load the file from disk:

// This will read a Kml file into memory.
KmlFile file = KmlFile.Load("YourKmlFile.kml");

// Kmz (compressed Kml files) can also be loaded:
KmzFile kmz = KmzFile.Open("YourKmzFile.kmz");
KmlFile file = KmlFile.LoadFromKmz(kmz);

Once the file is loaded you can iterate over the elements inside. Here's a quick example that uses Linq:

// Make sure these are placed at the top of your file:
// using System.Linq;
// using SharpKml.Engine;

// It's good practice for the root element of the file to be a Kml element, though not compulsary
Kml kml = file.Root as Kml;
if (kml != null)
    foreach (var placemark in kml.Flatten().OfType<Placemark>())

Last edited May 11, 2013 at 12:05 PM by samcragg, version 1


lpieraut Nov 25, 2016 at 12:53 AM 
when using kml files, make sure to have as header (and replace your header with the following 2 lines if not)
<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="">
otherwise the file will not be loaded with the function KmlLoad, and file.Root will always return null

LazaroVG Nov 9, 2016 at 10:43 PM 
Sorry, I missed add reference to System.IO and System.Runtime correct version (for my case 4.0)

LazaroVG Nov 9, 2016 at 9:46 PM 
spacewarper: since KmlFile.Load(string filename) is not even present in the KmlFile class, How can I Read a kmlfile I using:
StreamReader sr = new StreamReader(ofd.FileName,true);
KmlFile kmlin = KmlFile.Load((Stream)sr.BaseStream);

but compiler return error:

Argument 1: cannot convert from 'System.IO.Stream [c:\Windows\Microsoft.NET\Framework\v4.0.30319\mscorlib.dll]' to 'System.IO.Stream' (CS1503) - D:\Proys\oazPuntos\oazPuntos\Complemento.cs:118,35

cruster May 2, 2016 at 7:49 PM 
KmlFile.LoadFromKmz() is no longer available in latest version of the library.
kmz.GetDefaultKmlFile() should be probably used instead.

spacewarper Feb 22, 2016 at 3:04 PM 
Some more examples would help a lot.

spacewarper Feb 22, 2016 at 12:11 PM 
KmlFile.Load(string filename) is not even present in the KmlFile class.

milosko Sep 4, 2015 at 12:25 PM 
I know this comes late but may serve for someone else...

Placemark pm = some placemark loaded from kml file....
var point = pm.Geometry as Point;
Console.WriteLine("Point: {0} {1}", point.Coordinate.Longitude, point.Coordinate.Latitude);

Sudhanthira Dec 10, 2013 at 11:55 AM 

I unable to add this sharpkml.dll file in my windows phone 8 app reference. I got following message "A reference to a higher version or incompatible assembly cannot be added to the project".


Mauser857 Jul 23, 2013 at 11:07 PM 
I have a usual kml file, and it gives null for file.Root as kml.
Can you help me if I send you the kml file?


shreydesai May 13, 2013 at 1:44 PM 
I am able to iterate through all the elements of placemark but not able to get the coordinates of the placemark since there is no property like placemark.Coordinate.Latitude or something similar.

So code to extract the coordinates would be helpful.

Thanks in advance.