This project has moved. For the latest updates, please go here.

Saving a Kml file without kml: namespace prefix

Sep 16, 2014 at 6:26 AM
Edited Sep 16, 2014 at 6:39 AM
If I load a kml file that was saved from Google Earth. KmlFile kml = KmlFile.Load(filename) and save this kml file with kml.Save(backupFilename) I get a prefix (kml:) in every tag of the resulted kml file. This appears to happen because of the namespaces in the kml file. <kml xmlns="" xmlns:gx="" xmlns:kml="" xmlns:atom=""> Note that the default namespace name the kml name space have the same Uri. This does seem to be a problem of the XmlWriter as I tested with just an XmlWriter and got the same result. How can I get around this? Is it possible to remove the xmlns:kml namespace when the file is loaded? Thanks Garry
Sep 16, 2014 at 7:03 PM
You might be able to remove the prefix from the Namespaces property of Element, via the RemoveNamespace method, however, the property is currently internal so you will either need to modify the source code or get it via reflection.

Recently I added the AddNamespacePrefix method to the Kml class, which adds a namespace and prefix, perhaps another method would help to remove a prefix?

Let me know if that doesn't work and I'll take a more detailed look,
Marked as answer by dixong on 9/16/2014 at 1:28 PM
Sep 16, 2014 at 8:33 PM
Hi Sam,

RemoveNamespace did the job, I tested by modifying the source to make my life a little easier as below.
        public void RemoveNamespacePrefix(string prefix)
            if (prefix == null)
                throw new ArgumentNullException("prefix");

            if ((prefix == string.Empty) ||
                (prefix == "xml") ||
                (prefix == "xmlns"))
                throw new ArgumentException("Invalid prefix.", "prefix");

            string uri = this.Namespaces.LookupNamespace(prefix);

            if (uri != null)
                this.Namespaces.RemoveNamespace(prefix, uri);
So now I can read in a file, modify it, perform a RemoveNamespacePrefix("kml") and then save it. Although I've lost the kml namespace generated by Google Earth it is still a valid kml file that can be read by any other application that needs it.