in order to Remove all Attributes just call the .RemoveAllAttributes()
method on every Node, but be careful: Attributes with the name xlmns
are not treated like normal attributes. These are part of the namespace-stuff, and you need to remove them in a different way:
Answer to: C# How to remove namespace information from XML elements
string xmlPath = @"D:\test.xml";
XDocument d = XDocument.Load(xmlPath);
var allNodes = d.Descendants();
foreach (var node in allNodes)
{
//Removes ALL attributes except namespace attributes like 'xmlns="..."'
node.RemoveAttributes();
}
To remove the declaration at the beginning:
//Instead of using XDocument.Save() , use XmlWrite to Remove the declaration
XmlWriterSettings xws = new XmlWriterSettings();
xws.OmitXmlDeclaration = true;
xws.Indent = true;
using (var stream = File.Create(@"D:\testNew.xml"))
using (XmlWriter xw = XmlWriter.Create(stream, xws))
{
d.Save(xw);
}