Reputation: 221
I'm was trying to write a quick and dirty console application utility to find ObjectName in file and then set all occurrences of Application Name to the ObjectName. (these are SSIS packages (xml))
Object name is a simple string [ DTS:ObjectName="ETL Hbhc Receptive Communication" ] But Application Name is embedded in a connection string. (shown below before and after)
I am not getting desired result from the Replace statement and something is occurring at end of file that shows up in a file compare. When I try and run the SSIS package in SQL Server I get an invalid xml format error. The only thing I can see visibly is the quotation marks around the replacement text. I presume this is the problem so how do I replace text without the quotation marks??
Diff checker also flags Last line of file as being different but I don't see anything visibly. Both have as text.
Differences: Before edit:
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;" />
After edit:
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;" />
Application Code:
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]);
}
}
}
Upvotes: 0
Views: 75
Reputation: 13975
OK, I think I see the problem.
When you encounter this string in your files:
DTS:ObjectName="ETL Hbhc Receptive Communication"
... you're using substring to get everything after the =
sign.
"Everything" includes the quotation marks in the original text. So rather than this:
ETL Hbhc Receptive Communication
... which would look like "ETL Hbhc Receptive Communication"
if it were a C# literal, you have this:
"ETL Hbhc Receptive Communication"
... which would look like "\"ETL Hbhc Receptive Communication\""
as a C# literal.
If you remove the first and last characters from the string you extract, or if you modify your substringing code to allow for the first and last characters, you should be OK.
Upvotes: 1