Adding namespaces to XmlDocument using XmlNamespaceManager

user3062349 picture user3062349 · Dec 18, 2013 · Viewed 14.7k times · Source

I am trying to add namespaces to an XmlDocument using XmlNamespaceManager. This is the current xml:

<?xml version="1.0"?>
<kml>
  <Document>
    <Placemark>
    </Placemark>
  </Document>
</kml>

I would like it to transform to this xml (using XmlNamespaceManager):

<?xml version="1.0"?>
<kml xmlns="http://www.opengis.net/kml/2.2"
     xmlns:gx="http://www.google.com/kml/ext/2.2" 
     xmlns:kml="http://www.opengis.net/kml/2.2"
     xmlns:atom="http://www.w3.org/2005/Atom"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <Document>
    <Placemark>
    </Placemark>
  </Document>
</kml>

But I am not able to change the xml. Here is the code, I know it should be an easy fix:

public void addXmlns()
        {

            string xml = @"<?xml version=""1.0""?>
                    <kml>
                    <Document>
                    <Placemark>
                    </Placemark>
                    </Document>
                    </kml>";

            var xmldoc = new XmlDocument();

            xmldoc.LoadXml(xml);

            XmlNamespaceManager nsmgr = new XmlNamespaceManager(xmldoc.NameTable);

            //Add the namespaces
            nsmgr.AddNamespace("", "http://www.opengis.net/kml/2.2");
            nsmgr.AddNamespace("gx", "http://www.google.com/kml/ext/2.2");
            nsmgr.AddNamespace("kml", "http://www.opengis.net/kml/2.2");
            nsmgr.AddNamespace("atom", "http://www.w3.org/2005/Atom");
            nsmgr.AddNamespace("xsi", "http://www.w3.org/2001/XMLSchema-instance");

            string message;
            message = xmldoc.InnerXml;

            MessageBox.Show(message); // still shows the original xml

        }

Thanks Before Hand

Update #1 - Tried this but it also does not change the XML:

public void addXmlns()
        {

            string xml = @"<?xml version=""1.0""?>
                    <kml>
                    <Document>
                    <Placemark>
                    </Placemark>
                    </Document>
                    </kml>";

            var xmldoc = new XmlDocument();

            xmldoc.LoadXml(xml);

            XmlSchema schema = new XmlSchema();
            schema.Namespaces.Add("", "http://www.opengis.net/kml/2.2");
            schema.Namespaces.Add("gx", "http://www.google.com/kml/ext/2.2");
            schema.Namespaces.Add("kml", "http://www.opengis.net/kml/2.2");
            schema.Namespaces.Add("atom", "http://www.w3.org/2005/Atom");
            schema.Namespaces.Add("xsi", "http://www.w3.org/2001/XMLSchema-instance");
            xmldoc.Schemas.Add(schema);

            string message;
            message = xmldoc.InnerXml;

            MessageBox.Show(message); // still shows the original xml

        }

Answer

user3062349 picture user3062349 · Dec 19, 2013

Solution: This finally worked:

public void addXmlns()
{
    string xml = @"<?xml version=""1.0""?>
                    <kml>
                    <Document>
                    <Placemark>
                    </Placemark>
                    </Document>
                    </kml>";

    var xmldoc = new XmlDocument();

    xmldoc.LoadXml(xml);

    xmldoc.DocumentElement.SetAttribute("xmlns", "http://www.opengis.net/kml/2.2");
    xmldoc.DocumentElement.SetAttribute("xmlns:gx", "http://www.google.com/kml/ext/2.2");
    xmldoc.DocumentElement.SetAttribute("xmlns:kml", "http://www.opengis.net/kml/2.2");
    xmldoc.DocumentElement.SetAttribute("xmlns:atom", "http://www.w3.org/2005/Atom");
    xmldoc.DocumentElement.SetAttribute("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance");

    string message;
    message = xmldoc.InnerXml;

    MessageBox.Show(message); // shows the updated xml  
}