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
?
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.
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.
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.
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.
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' Name
s. 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));
}
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