The OGC KML specification requires the http://www.opengis.com/kml/2.2 namespace. However, older or non-standard files may use different namespaces, such as http://earth.google.com/kml/2.2.
To handle these files, use the Parser class and call ParseString with the ignoreNamespaces parameter set to true. After parsing, wrap the parser.Root element in a KmlFile using KmlFile.Create(parser.Root, true) to restore a usable object model.
Note: When you save a file parsed this way, the OGC namespace will be used, overwriting the original non-standard namespace. This behavior is consistent with libkml.
using System;
using SharpKml.Base;
using SharpKml.Dom;
using SharpKml.Engine;
class Program
{
const string Xml =
"<kml xmlns='http://earth.google.com/kml/2.2'>" +
"<Placemark>" +
"<name>SamplePlacemark</name>" +
"</Placemark>" +
"</kml>";
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"
}
}