Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to read special character like é, â and others in C#

I can't read those special characters I tried like this

1st way #

string xmlFile = File.ReadAllText(fileName);

2nd way #

FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read);
StreamReader r = new StreamReader(fs);
string s = r.ReadToEnd();

But both statements don't understand those special characters. How should I read?

UPDATE ###

I also try all encoding with

string xmlFile = File.ReadAllText(fileName, Encoding. );

but still don't understand those special characters.

like image 861
kevin Avatar asked Nov 11 '11 03:11

kevin


3 Answers

There is no such thing as "special character". What those likely are is extended ascii characters from the latin1 set (iso-8859-1). You can read those by supplying encoding explicitly to the stream reader (otherwise it will assume UTF8)

using (StreamReader r = new StreamReader(fileName, Encoding.GetEncoding("iso-8859-1")))
    r.ReadToEnd();
like image 54
Ilia G Avatar answered Nov 13 '22 14:11

Ilia G


StreamReader sr = new StreamReader(stream, Encoding.UTF8)
like image 7
Kakashi Avatar answered Nov 13 '22 14:11

Kakashi


You have to tell the StreamReader that you are reading Unicode like so

StreamReader sr = new StreamReader(stream, Encoding.Unicode);

If your file is of some other encoding, specify it as the second parameter

like image 3
parapura rajkumar Avatar answered Nov 13 '22 16:11

parapura rajkumar