Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
251 views
in Technique[技术] by (71.8m points)

(Recursive?) Regex to remove empty xml tags

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...