What's the easiest way to remove all attributes from a XML in C#?
I want to remove the attributes of all tags from a XML (I want to keep only the tags and their inner value). What's the easiest way to do th开发者_运维知识库is in C#?
static void removeAllAttributes(XDocument doc)
{
foreach (var des in doc.Descendants())
des.RemoveAttributes();
}
Usage:
var doc = XDocument.Load(path); //Or .Parse("xml");
removeAllAttributes(doc);
string res = doc.ToString();
foreach (XmlElement el in nodes.SelectNodes(".//*")) {
el.Attributes.RemoveAll();
}
It's faster if you use a better XPath like "//*[@*]"
foreach (XmlElement el in nodes.SelectNodes("//*[@*]")) {
el.Attributes.RemoveAll();
}
This will limit the results to just elements that have attributes.
精彩评论