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));
}