Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to prevent blank xmlns attributes in output from .NET's XmlDocument?

People also ask

How to add attribute in xml using c#?

Xml; [...] //Assuming you have an XmlNode called node XmlNode node; [...] //Get the document object XmlDocument doc = node. OwnerDocument; //Create a new attribute XmlAttribute attr = doc. CreateAttribute("attributeName"); attr. Value = "valueOfTheAttribute"; //Add the attribute to the node node.

Why xmlns is added?

xmlns="" doesn't add a namespace, it removes one. It's been put there because you created an element that isn't in the same namespace as its parent.

What is the purpose of xmlns attribute?

The xmlns attribute specifies the xml namespace for a document. Note: The xmlns attribute is required in XHTML, invalid in HTML 4.01, and optional in HTML5. Note: The HTML validator at http://w3.org does not complain when the xmlns attribute is missing in an XHTML document.

What is namespace in @xmlelement?

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.


Thanks to Jeremy Lew's answer and a bit more playing around, I figured out how to remove blank xmlns attributes: pass in the root node's namespace when creating any child node you want not to have a prefix on. Using a namespace without a prefix at the root means that you need to use that same namespace on child elements for them to also not have prefixes.

Fixed Code:

XmlDocument xml = new XmlDocument();
xml.AppendChild(xml.CreateElement("root", "whatever:name-space-1.0"));
xml.DocumentElement.AppendChild(xml.CreateElement("loner", "whatever:name-space-1.0")); 
Console.WriteLine(xml.OuterXml);

Thanks everyone to all your answers which led me in the right direction!


This is a variant of JeniT's answer (Thank you very very much btw!)

XmlElement new_element = doc.CreateElement("Foo", doc.DocumentElement.NamespaceURI);

This eliminates having to copy or repeat the namespace everywhere.


If the <loner> element in your sample XML didn't have the xmlns default namespace declaration on it, then it would be in the whatever:name-space-1.0 namespace rather than being in no namespace. If that's what you want, you need to create the element in that namespace:

xml.CreateElement("loner", "whatever:name-space-1.0")

If you want the <loner> element to be in no namespace, then the XML that's been produced is exactly what you need, and you shouldn't worry about the xmlns attribute that's been added automatically for you.


Since root is in an unprefixed namespace, any child of root that wants to be un-namespaced has to be output like your example. The solution would be to prefix the root element like so:

<w:root xmlns:w="whatever:name-space-1.0">
   <loner/>
</w:root>

code:

XmlDocument doc = new XmlDocument();
XmlElement root = doc.CreateElement( "w", "root", "whatever:name-space-1.0" );
doc.AppendChild( root );
root.AppendChild( doc.CreateElement( "loner" ) );
Console.WriteLine(doc.OuterXml);