替换 xml 文件中的文本时没有得到所需的结果

本文关键字:结果 文件 xml 文本 替换 | 更新日期: 2023-09-27 18:37:25

我正在尝试编写一个快速而脏的控制台应用程序实用程序来查找文件中的ObjectName,然后将所有出现的应用程序名称设置为ObjectName。(这些是 SSIS 包 (xml))

对象名称是一个简单的字符串 [DTS:ObjectName="ETL Hbhc 接受通信" ] 但应用程序名称嵌入在连接字符串中。(前后显示)

我没有从 Replace 语句中获得所需的结果,并且在文件末尾发生了一些事情,显示在文件比较中。 当我尝试在 SQL Server 中运行 SSIS 包时,出现无效的 xml 格式错误。 我唯一能明显看到的是替换文本周围的引号。 我认为这是问题所在,那么如何替换不带引号的文本?

差异检查器还将文件的最后一行标记为不同,但我没有看到任何明显的东西。 两者都有作为文本。

差异:编辑前:

  DTS:ConnectionString="Data Source=r04phidwh62;Initial Catalog=v5dwst;Provider=SQLNCLI11;Integrated Security=SSPI;Auto Translate=False;Application Name=SSIS-Laboratory Fact Daily-{3CFA3DD8-2A24-40EB-8303-F3BC71507735}V5DWST;" />

编辑后:

DTS:ConnectionString="Data Source=r04phidwh62;Initial Catalog=v5dwst;Provider=SQLNCLI11;Integrated Security=SSPI;Auto Translate=False;Application Name="ETL Hbhc Receptive Communication"-{3CFA3DD8-2A24-40EB-8303-F3BC71507735}V5DWST;" /> 

应用代码:

static void Main(string[] args)
        {
            string[] files = Directory.GetFiles(@"d:'Transforms'", "*.dtsx")
                                     .Select(path => Path.GetFileName(path))
                                     .ToArray();
            int counter = 0;
            string line;
            for (int i = 0; i < files.Length; i++)
            {
                using (var outputfile = new StreamWriter(@"D:'Transforms'output.txt"))
                {
                    // Read the file and display it line by line.
                    System.IO.StreamReader file =
                    new System.IO.StreamReader(@"D:'Transforms'" + files[i]);
                    string test = "";
                    while ((line = file.ReadLine()) != null)
                    {
                        if (line.Contains("DTS:ObjectName"))
                        {
                            if (test.Length == 0)
                            {
                                test = line.Substring(line.IndexOf("=") + 1);
                                System.Console.WriteLine(test);
                            }
                        }
                        if (line.Contains("Application Name"))
                        {
                            string output = line.Substring(line.IndexOf("Application Name=") + 17);
                            if (output.Contains("-{"))
                            {
                                output = output.Substring(0, output.IndexOf("-{"));
                            }
                            else
                            {
                                output = output.Substring(0, output.IndexOf(";"));
                            }
                            System.Console.WriteLine(output);
                            line = Regex.Replace(line, output , test);
                        }
                        outputfile.WriteLine(line);
                        counter++;
                    }
                    file.Close();
                    outputfile.Close();
                    // Suspend the screen.
                    System.Console.ReadLine();
                    File.Delete(@"D:'Transforms'" + files[i]);
                    File.Move(@"D:'Transforms'output.txt", @"D:'Transforms'" + files[i]);
                }
            } 
        }

替换 xml 文件中的文本时没有得到所需的结果

好的,我想我看到了问题。

当您在文件中遇到此字符串时:

 DTS:ObjectName="ETL Hbhc Receptive Communication"

。你正在使用子字符串来获取=符号之后的所有内容。

"一切"包括原文中的引号。 所以而不是这个:

ETL Hbhc Receptive Communication

。看起来像"ETL Hbhc Receptive Communication"如果是 C# 文字,你有这个:

"ETL Hbhc Receptive Communication"

。看起来像"'"ETL Hbhc Receptive Communication'"" C# 文本。

如果从提取的字符串中删除第一个和最后一个字符,

或者修改子字符串代码以允许使用第一个和最后一个字符,则应该没问题。