Linq to xml - get rid of empty xmlns

I am trying to get rid of empty namespace tags in my XML file. All the solutions I've seen are based on creating xml from scratch. I have various xelements built from the previous xml. All i do is

XElement InputNodes = XElement.Parse(InputXML); m_Command = InputNodes.Element("Command"); 

and he adds xmlns = "" everywhere. It really pisses me off. Thanks for any help.

+4
source share
3 answers

There's a post on MSDN blogs that shows how easy it is to get around this (reasonably). Before you output XML, you will want to execute this code:

 foreach (XElement e in root.DescendantsAndSelf()) { if (e.Name.Namespace == string.Empty) { e.Name = ns + e.Name.LocalName; } } 

The alternative, as the poster mentions, is to prefix each element name with a namespace as it is added, but this seems like a nicer solution, as it is more automated and saves a bit of input.

+8
source

Perhaps this is: An empty namespace using Linq Xml

This means that your document is in a different default namespace than the elements you add.

0
source

I think the second answer to this post:

XElement add function adds xmlns = "" to XElement

was very helpful. Basically, if you just do

 XNamespace rootNamespace = doc.Root.Name.NamespaceName; XElement referenceElement = new XElement(rootNamespace + "Reference"); 

That should decide. Therefore, I think you should tell him not to worry about a special namespace when you create an element. Odd

0
source

All Articles