Remove empty XML tags

后端 未结 6 1394
执笔经年
执笔经年 2020-12-09 18:39

I am looking for a good approach that can remove empty tags from XML efficiently. What do you recommend? Regex? XDocument? XmlTextReader?

For example,



        
6条回答
  •  北海茫月
    2020-12-09 19:33

    Loading your original into an XDocument and using the following code gives your desired output:

    var document = XDocument.Parse(original);
    document.Descendants()
            .Where(e => e.IsEmpty || String.IsNullOrWhiteSpace(e.Value))
            .Remove();
    

提交回复
热议问题