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

只愿长相守 提交于 2019-11-27 02:08:49

问题


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.


回答1:


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();



回答2:


StreamReader sr = new StreamReader(stream, Encoding.UTF8)



回答3:


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



来源:https://stackoverflow.com/questions/8089357/how-to-read-special-character-like-%c3%a9-%c3%a2-and-others-in-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!