7

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

        }
user3062349
  • 691
  • 2
  • 7
  • 15

2 Answers2

8

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  
}
Flat Eric
  • 7,971
  • 9
  • 36
  • 45
user3062349
  • 691
  • 2
  • 7
  • 15
  • I'm curious how you're getting around the issue I'm running into... When you use the default namespace (xmlns), then add another suffixed namespace (xmlns:gx), I'm getting the following error: `The prefix '' cannot be redefined from '' to 'http://...' within the same start element tag.` – shortstuffsushi Jan 19 '16 at 17:11
2

If you have some knowledge of XML/XPath/XQuery, you will get comfortable with this behavior.

In short, the namespace manager is for XPath, SelectNodes/SelectSingleNodes specifically; it is not intended for generating XML.

EDIT: The namespaces added to the manager instance is a way to tell XPath that in a query string like this /doc/mynamespace:mynode, how to explain and scope in various contexts.

nim
  • 384
  • 2
  • 14
  • Thanks for pointing out that XmlNamespaceManager is not intended for this purpose. What would be the correct way to add namespaces to an existing XmlDocument that is already loaded? (Without using XSLT) – user3062349 Dec 18 '13 at 13:37
  • See this thread: http://stackoverflow.com/questions/2920142/how-to-add-xmlnamespace-to-a-xmldocument – nim Dec 18 '13 at 13:38
  • Thanks nim that did it! See solution above. – user3062349 Dec 18 '13 at 14:04