简体   繁体   中英

How do I find a particular pattern string in a pack of XML files and fix them for missing Tags in C#

The requirement I'm trying to achieve is quite complicated and I'm not able to think beyond at certain point.

1) I need to traverse through a list of some thousands of files and folders(typically complex XMLs) and find a particular string pattern like { DisplayKey.get(" } (forget the parentheses) and replace them with { DisplayKey.get(& quot ; }. -> Thats Obvious and Easy

2) Now here is the tougher part. The Ideal way the above said text should exist in the XML in any tag is like the pattern below:

DisplayKey.get("Web.Admin.MessageDestinationStatisticsDV.Failed")

The ideal pattern goes this way DisplayKey.get("xxx.xxx.xxx.xxx.xxx") where x could be any string and the pattern should end with ").

My code should identify the sequences that starts with { DisplayKey.get(" } that does NOT end with { ") } and fix it.

Below is the approach I started:

static void WalkDirectoryTree(DirectoryInfo root) { FileInfo[] files = null; DirectoryInfo[] subDirs = null; files = root.GetFiles(" . ");

        if (files != null)
        {
            try
            {
                foreach (FileInfo fi in files)
                {
                    String errDSTR = "DisplayKey.get(\"";
                    string[] allLines = File.ReadAllLines(fi.FullName);
                    var writer = new StreamWriter(fi.FullName);
                    for (int i = 0; i < allLines.Length; i++)
                    {
                        string line = allLines[i];

                        // Find DisplayKey.get("
                        // Replace it with DisplayKey.get(&quot;
                        // LOGIC: HOW DO I APPROACH THIS?
                        foreach(char ch in line.ToCharArray())
                        {
                          //Sadly .IndexOf() only finds the First String and not the subsequet ones
                        }                        
                }
            }
            catch(Exception e)
            {
                Console.WriteLine("Exception Occured :" + e.Message);
                Console.ReadLine();
            }                
            subDirs = root.GetDirectories();

            foreach (System.IO.DirectoryInfo dirInfo in subDirs)
            {
                // Resursive call for each subdirectory.
                WalkDirectoryTree(dirInfo);
            }
        }
    } 

I know File.WriteAllText(fi.FullName, File.ReadAllText(fi.FullName).Replace("some text", "some other text")); could address a generic text but I'm wondering how to I traverse through and fix the pattern issue!

An approach you could take is to use regex matching to make to checks:

  1. Check if the line contains ' DisplayKey.get(" ' . Use the regex DisplayKey\\.get\\(" (note the escape chars)

  2. Check if the line does not contain an element of the form DisplayKey.get("....."). Use the regex DisplayKey\\.get\\(".+"\\). The .+ part of the regex matches any number of characters between the parenthesis.

  3. For each line where there is a match for 1 and there isn't a match for 2, append )" at the end.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM