40

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.

John Saunders
  • 160,644
  • 26
  • 247
  • 397
kevin
  • 13,559
  • 30
  • 79
  • 104
  • 1
    how do you know your program isn't accepting those characters? what are you doing with them? if you're spitting them back out to the console it could be that your terminal isn't displaying them correctly, but you're reading them just fine. – mpen Nov 11 '11 at 03:38
  • 1
    What is not read correctly? Show a line of sample text with data that is read wrong, the result you observe, the result you expect, and how you made the observation. – Joel Coehoorn Nov 11 '11 at 03:39

6 Answers6

112

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();
Ilia G
  • 10,043
  • 2
  • 40
  • 59
  • 6
    string[] lines = File.ReadAllLines(fileName,System.Text.Encoding.GetEncoding("iso-8859-1")); also works perfect – Muhammad Rehan Qadri Jan 21 '16 at 04:08
  • This solves my problem, but I do not understand why. In my case my streamreader replaced 'ë' with '�' when going through a json file (when parsing with UTF-8). Why though, UTF-8 surely knowns the character 'ë', and json does not have an encoding I know of? Or does this have to do with .NET runtime or something? – Daniël Camps Mar 15 '19 at 13:49
  • 1
    After applying this, my problem solved for multiplication and plus sign, but apostrophe + S ('s) is not display properly, it replaced with square this sign. how to fix this? – Brijesh Jun 19 '19 at 07:00
7
StreamReader sr = new StreamReader(stream, Encoding.UTF8)
Kakashi
  • 2,165
  • 14
  • 19
  • 1
    StreamReader uses UTF8 by default. – Ilia G Nov 11 '11 at 03:43
  • yeah, I know that. but I tried read an file text containing accents and etc and the returns it something like: ?? instead of 'á' for example. I setting the coding to utf8(as suggested here) and this worked fine. – Kakashi Nov 11 '11 at 03:53
  • 1
    hmm I am not sure... I would have to look at the code and actual file to see what was going on there. But but according to this http://msdn.microsoft.com/en-us/library/f2ke0fzy.aspx StreamREader init with UTF8 encoding by default and that has been the case as far as I tried. – Ilia G Nov 11 '11 at 04:02
4

This worked for me :

var json = System.IO.File.ReadAllText(@"././response/response.json" , System.Text.Encoding.GetEncoding("iso-8859-1"));
Jaydeep Shil
  • 1,894
  • 22
  • 21
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

parapura rajkumar
  • 24,045
  • 1
  • 55
  • 85
0

I had to "find" the encoding of the file first

//try to "find" the encoding, if not found, use UTF8
var enc = GetEncoding(filePath)??Encoding.UTF8;
var text = File.ReadAllText(filePath, enc );

(please refer to this answer to get the GetEncoding function)

The One
  • 4,560
  • 5
  • 36
  • 52
0

If you can modify the file in question, you can save it with encoding.

I had a json file that I had created (normally) in VS, and I was having the same problem. Rather than specify the encoding when reading the file (I was using System.IO.File.ReadAllText which defaults to UTF8), I resaved the file (File->Save As) and on the Save button, I clicked the arrow and chose "Save with Encoding", then chose "Unicode (UTF-8 with signature) - Codepage 65001".

Problem solved, no need to specify the encoding when reading the file.

Marc Levesque
  • 154
  • 1
  • 6