Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove xmlns attribute from XDocument?

In my C# codebase, I have an XDocument of the form:

<A>
 <B>
   <C xmlns='blabla' yz='blablaaa'> Hi </C>
   <D xmlns='blabla' yz='blablaaa'> How </D>
   <E xmlns='blabla' yz='blablaaa'> Are </E>
   <F xmlns='blabla' yz='blablaaa'> You </F>
 </B>
 <B>
   <C xmlns='blabla' yz='blablaaa'> I </C>
   <D xmlns='blabla' yz='blablaaa'> am</D>
   <E xmlns='blabla' yz='blablaaa'> fine</E>
    <F xmlns='blabla' yz='blablaaa'> thanks</F>
 </B>

Using Linq-to-XML or otherwise, I wanted to remove the xmlns for all the elements contained by element B.

Using the methodology given here: How to Remove specific attributes in XMLDocument?, I was able to remove all attributes except xmlns

What is the best way to remove 'xmlns' attribute from XDocument?

like image 334
GilliVilla Avatar asked Aug 09 '11 22:08

GilliVilla


People also ask

How do I get rid of xmlns?

Open the map in the Design Studio. Edit the output card in question (in this case output card 1 of the validationMap map) and expand it so you can right click on Property > Schema > Type > Metadata > Name Spaces > http://www.example.com/IPO. After right clicking on http://www.example.com/IPO, left click on Delete.

Why xmlns is added?

The xmlns attribute in the first <table> element gives the h: prefix a qualified namespace. The xmlns attribute in the second <table> element gives the f: prefix a qualified namespace. When a namespace is defined for an element, all child elements with the same prefix are associated with the same namespace.

What is xmlns attribute?

the xmlns attribute specifies the xml namespace for a document. This basically helps to avoid namespace conflicts between different xml documents, if for instance a developer mixes xml documents from different xml applications.

How use xmlns attribute?

Definition and UsageThe 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.


1 Answers

It seems the namespace information are kept in two places in the object tree that represents the XML document in LINQ to XML: as actual xmlns attributes and inside the elements' Names. If you remove it from both places it's gone:

doc.Descendants()
   .Attributes()
   .Where( x => x.IsNamespaceDeclaration )
   .Remove();

foreach (var elem in doc.Descendants())
    elem.Name = elem.Name.LocalName;

(The first part of the code above is copied from now deleted answer by Bertrand Marron.)

If you wanted to remove namespaces from attributes too, that's little more complicated, because their Name is read-only:

foreach (var attr in doc.Descendants().Attributes())
{
    var elem = attr.Parent;
    attr.Remove();
    elem.Add(new XAttribute(attr.Name.LocalName, attr.Value));
}
like image 200
svick Avatar answered Sep 24 '22 17:09

svick