What's the easiest way to remove all attribute

2019-04-07 08:01发布

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 this in C#?

2条回答
在下西门庆
2楼-- · 2019-04-07 08:49
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();
查看更多
可以哭但决不认输i
3楼-- · 2019-04-07 09:02
foreach (XmlElement el in nodes.SelectNodes(".//*")) {
   el.Attributes.RemoveAll();
}
查看更多
登录 后发表回答