HEXAQ : A friendly question - answer site for IT professionals

xmlns attribute added by default to each node


0

I am adding child nodes to an existing xml file. below is my code lines

XmlDocument xmlDoc = new XmlDocument();
                            xmlDoc.Load(Server.MapPath("~/sitemap.xml"));

                            XmlNode rootNode = xmlDoc.DocumentElement;
                            XmlNode urlNode = xmlDoc.CreateElement("car");
                            rootNode.AppendChild(urlNode);

it adds blank xmlns attribute to each node that I add through my code as below:

<?xml version="1.0" encoding="UTF-8"?>
<vehicles xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
  <car xmlns="">
       <color>black</color>
  </car>
</vehicles>

xmlns attribute in tag is added, that I want to eliminate.

How can I do that?

Â
asked
 


≡ Total 1 Answer

0

There is one more line of code required to eliminate the attribute added by default.

Check this code:

XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(Server.MapPath("~/sitemap.xml"));
XmlNode rootNode = xmlDoc.DocumentElement;
XmlNode urlNode = xmlDoc.CreateElement("url", xmlDoc.DocumentElement.NamespaceURI);
rootNode.AppendChild(urlNode);

Â
replied


Enter your reply here
Answer Preview: