XML : c# Looping through directory, finding XML in each directory and, if & present replace with & - Save

I would love to have the XML write correctly but the file is coming to us from a 3rd party, who we have no control over. (they are another vendor of our customer)

So with that in mind: They send us a zip of folders and sub-folders (in each of the sub folders there will be an XML) The XML which they send us has & in place of &amp (every & is like this but not every XML will have a &); Using my code I can (on console at least) loop through each of the subs, find the xml, find the line with the & (if there is one) and change it to &

  foreach (string d in Directory.GetFiles(targetDirectory, "*.xml", SearchOption.AllDirectories))              {                  Console.WriteLine(d);                  String[] lines = File.ReadAllLines(d);                  for (int i = 0; i < lines.Length; i++)                  {                      if (lines[i].Contains("&"))                      {                          string line = lines[i];                          line = line.Replace("&", "&amp;");                          Console.WriteLine(line);                        }                    }              }    

In the last "Console.WriteLine(line);" it shows that the & is changed (in memory at least) but when I use the "System.IO.File" if still populates with the original &.

No comments:

Post a Comment