PerfCSharp

PerfCSharp

  • NA
  • 5
  • 1.6k

c# Filesystem watcher to pick and convert XML encoding

Feb 14 2013 12:21 AM

I'm using the filesystem watcher to pick and convert the encoding of a file, the file gets copied at the destination but without conversion. I was using the below code earlier:

string xml = File.ReadAllText(FileName,ansi);
       
XDocument xmlDoc = XDocument.Parse(xml);
            
Console.WriteLine("1st"); File.WriteAllText(
                
FileName, @"<?xml version=""1.0"" encoding=""utf-8""?>" +  xml.ToString(),
                   utf8
              
);


            
if (File.Exists(destinationFile))
                   
File.Delete(destinationFile);
               
File.Copy(FileName, destinationFile,true);
        
Console.WriteLine("File Copied" + "  " + DateTime.Now.ToString("HH:mm:ss tt"));                 Console.WriteLine("Press \'q\' to quit.");
               
Console.Write(CrL);

 But this is not working now , as the xml file I'm getting now contains the following header:

  <?xml version="1.0" encoding="WINDOWS-1256"?>

I was earlier suggested the below code:

var doc = new XmlDocument();
doc
.Load(FileName);

XmlWriterSettings settings = new XmlWriterSettings { Encoding = Encoding.UTF8, Indent=true };

using (var fileStream = File.OpenWrite(destinationFile))
{
using (var writer = XmlWriter.Create(fileStream, settings))
{

    doc
.Save(writer);
 
}
}



But this is not doing the conversion at all. The dest. file is still ansi. It's not doing the conversion to utf8. Am I doing something wrong here



Attachment: test.rar

Answers (1)