Click here to Skip to main content
15,891,951 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all,
I have a method that export data to a file(.txt or .csv). all works fine except the encoding.

the exported data contains caracters such as (é, à,...), those caractere appears incorrectly in same computers while it works fine in others.

here is the code.
C#
using (StreamWriter sWriter = new StreamWriter(stream, Encoding.UTF8))
{
   sWriter.WriteLine("Nom ; Prénom ; Adresse ; Code postal ; Ville ; Prospection ; Fixe1 ; Fixe2 ; Fax ; Portable");

   foreach (ExtendedContact c in extendedContactList)
   {
      if (cb_prospection.Checked)
      {
         if (c.Prospection != "Contre")
         {
            sWriter.Write(OneSpace(c.Nom) + ";" + OneSpace(c.Prenom) + ";" + OneSpace(c.Address) + ";" + OneSpace(c.CodePostal) + ";" + OneSpace(c.Ville) + ";" + OneSpace(c.Prospection) + ";" + OneSpace(c.Fixe1) + ";" + OneSpace(c.Fixe2) + ";" + OneSpace(c.Fax) + ";" + OneSpace(c.Portable));
            sWriter.WriteLine();
         }
      }
   }
}


I want to create a generic solution that works in any computer.
what do you think is the best solution? do i have to give the user to select between diffrent kind of encoding(i am totaly aginst since many user don't know what encoding is)?

Thanks
Posted
Updated 13-May-14 3:32am
v2
Comments
Richard MacCutchan 13-May-14 11:52am    
If you are using characters that are specific to a certain language, then anyone reading that file must use the same language set.
Ziee-M 13-May-14 11:57am    
Hi, the data i get from the database are In UTF8 Format, when i export the data to a file using streamWriter while specifing the encoding to UTF8, i get the issue in some computers. after some resarh, the issue shouldn't exsist in the first place, althought i am not sure!
Keith Barrow 13-May-14 12:24pm    
Are you sure the characters went in to the DB with the correct encoding? If not you might have problems. Additionally, the machine you are viewing on needs to support the characters, and the text transferred with the correct encoding (e.g. in an HTML page).
Ziee-M 13-May-14 12:35pm    
I am sure about the data saved in the db in UTF8.
According to many threads, some pepole had this issue, and its related to "ByteOrderMarking"(BOM) not created issue. i will try the solution founded for now :
using (StreamWriter sWriter = new StreamWriter(stream, new UTF8Encoding(true)))
instead of
using (StreamWriter sWriter = new StreamWriter(stream, Encoding.UTF8) )
phil.o 13-May-14 12:42pm    
Strange as Unicode standard does not enforce nor encourage the byte-order mark for UTF8 files.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900