What is the easiest way to remove all attributes from XML in C #?

I want to remove the attributes of all tags from XML (I want to save only the tags and their internal value). What is the easiest way to do this in C #?

+5
source share
2 answers
static void removeAllAttributes(XDocument doc)
{
    foreach (var des in doc.Descendants())
        des.RemoveAttributes();
}

Using:

var doc = XDocument.Load(path); //Or .Parse("xml");
removeAllAttributes(doc);

string res = doc.ToString();
+2
source

foreach (XmlElement el in .SelectNodes (".//*") nodes) {

el.Attributes.RemoveAll ();

}

+3
source

All Articles