The OGC KML specification specifies that Kml elements should be inside the namespace. However, before the specification was finalized, programs may have used different namespaces (a common example being and you may need to work with those files. One way of doing this is use a Parser as follows:

using System;
using SharpKml.Base;
using SharpKml.Dom;
using SharpKml.Engine;

class Program
    const string Xml =
        "<kml xmlns=''>" +
            "<Placemark>" +
                "<name>SamplePlacemark</name>" +
            "</Placemark>" +

    static KmlFile ParseLegacyFile()
        // Manually parse the Xml
        Parser parser = new Parser();
        parser.ParseString(Xml, false); // Ignore the namespaces

        // The element will be stored in parser.Root - wrap it inside
        // a KmlFile
        return KmlFile.Create(parser.Root, true);

    static void Main(string[] args)
        KmlFile file = ParseLegacyFile();

        // Prove it worked ok
        Kml kml = (Kml)file.Root;
        Placemark placemark = (Placemark)kml.Feature;
        Console.WriteLine(placemark.Name); // Outputs "SamplePlacemark"

Please note when the file is saved, the OGC namespace will be used, overwriting the original namespace (this is consistent with libkml).

Last edited Jun 1, 2011 at 8:25 PM by samcragg, version 1


No comments yet.