0

Hello I searched the internet this exception, I have not found solution so I decided to ask. in a console application I need to search a folder a certain number of xml files. such file is encoded in ANSI.   what the application does is to take the file and convert it to utf-8 encoding. but as the cycle goes catch this exception. Invalid character in the given encoding. Line 1, position 2757 I get the xml from a third alguna Idea? muchas gracias. this is my code

i get this exception in 3 xmls of 100

        int Count = 0;
        string sNombre = "";
        string targetPath = @"C:\SomePath\";
        string logError = "log_xml_Error.log";
        if (File.Exists(@"log_xml_Error.log"))
        {
            File.Delete(@"log_xml_Error.log");
        }

        Console.WriteLine("press enter");
        Console.ReadLine();

        string[] xmls = Directory.GetFiles(targetPath, "*.xml", SearchOption.AllDirectories);

        foreach (var sFile in xmls)
        {
            try
            {
                Count++;
                XmlDocument xmlDoc = new XmlDocument();

                xmlDoc.Load(sFile);
                byte[] sXmlByte = Encoding.UTF8.GetBytes(xmlDoc.OuterXml);

                sName = Path.GetFileName(sFile);
                string sCompletePathXML = @"C:\SomePath\" + sName;


                if (!File.Exists(sCompletePathXML))
                {
                    File.WriteAllText(sCompletePathXML, System.Text.UTF8Encoding.UTF8.GetString(sXmlByte), System.Text.UTF8Encoding.UTF8);
                }
                Console.WriteLine(Count);
            }
            catch (Exception ex)
            {
                using (StreamWriter sw = File.AppendText(logError))
                {
                    sw.WriteLine(" Error: " + ex.Message + "XML :" + sName);

                }
            }
        }

        Console.WriteLine("process complete");
        Console.ReadLine();
user3357141
  • 199
  • 1
  • 3
  • 16

1 Answers1

0

what the application does is to take the file and convert it to utf-8 encoding

I would simle do

var xdoc = XDocument.Parse(File.ReadAllText(filename,Encoding.ASCII));
File.WriteAllText(filename, xdoc.ToString(), Encoding.UTF8);
EZI
  • 15,209
  • 2
  • 27
  • 33