在C#中从XML中删除所有属性的最简单方法是什么?

时间:2021-09-26 14:03:31

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#?

我想从XML中删除所有标记的属性(我想只保留标记及其内部值)。在C#中最简单的方法是什么?

2 个解决方案

#1


2  

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

#2


3  

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

foreach(nodes.SelectNodes中的XmlElement el(“.//*”)){

el.Attributes.RemoveAll();

el.Attributes.RemoveAll();

}

}

#1


2  

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

#2


3  

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

foreach(nodes.SelectNodes中的XmlElement el(“.//*”)){

el.Attributes.RemoveAll();

el.Attributes.RemoveAll();

}

}