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

前端 未结 4 1980
旧巷少年郎
旧巷少年郎 2020-12-09 01:50

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

1st way #

string xmlFile = File.ReadAllText(fileName);

2nd way #

相关标签:
4条回答
  • 2020-12-09 01:57
    StreamReader sr = new StreamReader(stream, Encoding.UTF8)
    
    0 讨论(0)
  • 2020-12-09 02:03

    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();
    
    0 讨论(0)
  • 2020-12-09 02:14

    This worked for me :

    var json = System.IO.File.ReadAllText(@"././response/response.json" , System.Text.Encoding.GetEncoding("iso-8859-1"));
    
    0 讨论(0)
  • 2020-12-09 02:16

    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

    0 讨论(0)
提交回复
热议问题