Domanda

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

        }
È stato utile?

Soluzione

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  
}

Altri suggerimenti

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.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top