Remove empty XML tags

后端 未结 6 1402
执笔经年
执笔经年 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:10

    Anything you use will have to pass through the file once at least. If its just a single named tag that you know then regex is your friend otherwise use a stack approach. Start with parent tag and if it has a sub tag place it in stack. If you find an empty tag remove it then once you have gone through child tags and reached the ending tag of what you have on top of stack then pop it and check it as well. If its empty remove it as well. This way you can remove all empty tags including tags with empty children.

    If you are after a reg ex expression use this

提交回复
热议问题