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>())
    {
        Console.WriteLine(placemark.Name);
    }
}

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

Comments

lpieraut Nov 24, 2016 at 11:53 PM 
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="http://www.opengis.net/kml/2.2">
otherwise the file will not be loaded with the function KmlLoad, and file.Root will always return null

LazaroVG Nov 9, 2016 at 9: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 8:46 PM 
spacewarper: since KmlFile.Load(string filename) is not even present in the KmlFile class, How can I Read a kmlfile I using:
<code>
StreamReader sr = new StreamReader(ofd.FileName,true);
sr.BaseStream.InitializeLifetimeService();
KmlFile kmlin = KmlFile.Load((Stream)sr.BaseStream);
</code>

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 6: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 2:04 PM 
Some more examples would help a lot.

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

milosko Sep 4, 2015 at 11:25 AM 
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 10:55 AM 
Hi,

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".

Thanks.

Mauser857 Jul 23, 2013 at 10:07 PM 
Hi,
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?

thanks,
Laszlo

shreydesai May 13, 2013 at 12:44 PM 
Hello,
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.