Saturday, December 17, 2011

Handling Unicode Characters in ASP.NET

We working with Unicode characters here’s a quick insight on things that need to be taken care.

We always need ensure that the HTML pages that we generate(static or dynamic) need to have the following line, to ensure seamless capturing of Unicode characters.

   1: <META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=UTF-8"/>

If we try to save the html content with Unicode characters, then if we try to save it directly via file stream then the html content will not be preserved, so we’ll need to save it using the following method.



   1: //fs -> filestream writer
   2:  
   3: StreamWriter swlocal = new StreamWriter(fs, System.Text.Encoding.Unicode);
   4:                     swlocal.Write(html);
   5:                     swlocal.Flush();
   6:                     swlocal.Close();
   7: fs.Close();