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

        }

2 Answers 2

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  
}
Sign up to request clarification or add additional context in comments.

1 Comment

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.
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.

3 Comments

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)
Thanks nim that did it! See solution above.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.