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
}
XML Namespaces - The xmlns Attribute When using prefixes in XML, a namespace for the prefix must be defined. The namespace can be defined by an xmlns attribute in the start tag of an element. The namespace declaration has the following syntax. xmlns:prefix="URI".
To add namespaces to the namespace manager, you create a XmlNamespaceManager object and then use the AddNamespace method. Default prefix and namespace pairs are automatically added to the namespace manager on creation.
XML namespaces provide a method for qualifying the names of XML elements and XML attributes in XML documents. A qualified name consists of a prefix and a local name, separated by a colon. The prefix functions only as a placeholder; it is mapped to a URI that specifies a namespace.
An XML namespace is a collection of names that can be used as element or attribute names in an XML document. The namespace qualifies element names uniquely on the Web in order to avoid conflicts between elements with the same name.
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
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With