(Recursive?) Regex to remove empty xml tags

旧城冷巷雨未停 提交于 2019-12-13 07:48:54

问题


I'd like to remove all empty tags from an xml file. However, my options are very limited, so I'd like to use a regex (which is available and known internally here).

I have no problem with the regex to remove the empty tags in their variations, but the nested empty tags are a bit harder, as my regex will only go one deep.

I believe it's because of the named capture group in my recursion, but I'm not able to fix it.

This is what I have so far: here

Best regards and thanks,

Laurent


回答1:


Try this

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            string xml = 
                "<root>" +
                    "<tag1>Good</tag1>" +
                    "<tag2 element=\"Good\"></tag2>" +
                    "<tag3 element=\"Good\" />" +
                    "<tag4></tag4>" +
                    "<tag13>" +
                    "</tag13>" +
                    "<tag5 />" +
                    "<tag6/>" +
                    "<tag7>" +
                        "<tag7.1>good</tag7.1>" +
                    "</tag7>" +
                    "<tag8>" +
                        "<tag8.1></tag8.1>" +
                    "</tag8>" +
                    "<tag9>" +
                        "<tag9.1 />" +
                    "</tag9>" +
                    "<tag10>" +
                        "<tag10.1/>" +
                    "</tag10>" +
                    "<tag10>" +
                        "<tag10.1>Wel iets</tag10.1>" +
                    "</tag10>" +
                    "<tag11>" +
                        "<tag11.1 element=\"Good\"/>" +
                    "</tag11>" +
                    "<tag12>" +
                        "<tag12.1></tag12.1>" +
                        "<tag12.2>" +
                            "<tag12.2.1></tag12.2.1>" +
                        "</tag12.2>" +
                    "</tag12>" +
                    "</root>";
            XElement root = XElement.Parse(xml);
            var deleteElements = root.Descendants().Where(x => (x.Descendants().Count() == 0) && (x.Attributes().Count() == 0) && (x.Value.Length == 0)).ToList();
            foreach (XElement deleteElement in deleteElements)
            {
                deleteElement.Remove();
            }
        }
    }
}
​


来源:https://stackoverflow.com/questions/33919167/recursive-regex-to-remove-empty-xml-tags

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!