Damodar

Damodar

  • NA
  • 56
  • 0

Find out the encoding in an xml file using C#

Aug 6 2010 12:15 PM
Hello all,

I have an xml file which shows that the encoding in it is "encoding utf-16" but it is actually saved in utf-8. Actually saved means when i open the xml in a notepad and do saveas i get utf-8 in the encoding part in the save as window.

Now what I have to do is to write a program which first checks whether the encoding in the system is utf-8 or utf-16. If it is utf-8 then it has to change that to utf-16.

I tried using the Encoding to find out but it was showing me the encoding mentioned in the file, but i want the encoding in which the actual xml file is. i used

xmltextreader reader = new xmltextreader ("file");
encoding encode = reader.encoding; // this gives me the encoding in the file.

Will System.Text.Encoding class help me in this regard. Or should i use some System property to check the encoding of the xml file..

Please help me. Thank you in advance.