如何在一组XML文件中找到特定的模式字符串,并在C#中修复缺少的标记

时间:2022-10-16 07:20:43

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

1)我需要遍历数千个文件和文件夹(通常是复杂的XML)的列表,并找到特定的字符串模式,如{DisplayKey.get(“}(忘记括号),并用{DisplayKey.get(&)替换它们“}。 - >这显然很容易

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:

2)现在这里是更难的部分。上述文本的理想方式应该存在于任何标记的XML中,如下图所示:

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 ").

理想的模式就是DisplayKey.get(“xxx.xxx.xxx.xxx.xxx”),其中x可以是任何字符串,模式应该以“)结束。

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

我的代码应该识别以{DisplayKey.get(“}不以{”)}结尾的序列并修复它。

Below is the approach I started:

以下是我开始的方法:

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

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!

我知道File.WriteAllText(fi.FullName,File.ReadAllText(fi.FullName).Replace(“some text”,“some some text”));可以解决一般文本,但我想知道如何遍历和修复模式问题!

1 个解决方案

#1


0  

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)

    检查该行是否包含'DisplayKey.get(''。使用正则表达式DisplayKey \ .get \(“(注意转义字符)

  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.

    检查该行是否不包含DisplayKey.get(“.....”)形式的元素。使用正则表达式DisplayKey \ .get \(“。+”\)。正则表达式的。+部分匹配括号之间的任意数量的字符。

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

    对于匹配为1并且不匹配2的每一行,追加)“在最后。

#1


0  

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)

    检查该行是否包含'DisplayKey.get(''。使用正则表达式DisplayKey \ .get \(“(注意转义字符)

  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.

    检查该行是否不包含DisplayKey.get(“.....”)形式的元素。使用正则表达式DisplayKey \ .get \(“。+”\)。正则表达式的。+部分匹配括号之间的任意数量的字符。

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

    对于匹配为1并且不匹配2的每一行,追加)“在最后。