2

I have an application that reads information from a CSV file to write it to the database. But some characters (example: º ç) are appearing problems Gravalos base. Anyone know how to fix this problem? Thank you.

I'm using these lines of code to read the information from the CSV file:

string directory = @"C:\test.csv";
StreamReader stream = new StreamReader(directory);
string line = "";
line = stream.ReadLine();
string[] column = line.Split(';');
1
  • There is a csv parser in .NET. link I suggest you use it instead of trying to roll your own Commented Mar 20, 2014 at 20:19

3 Answers 3

2

StreamReader defaults to UTF8 encoding and your file is in a different encoding. Try specifying it like this...

    var encoding = Encoding.UTF16;
    StreamReader stream = new StreamReader(directory, encoding);

Note that you need to know what encoding the file is in to properly read it... I'm just guessing that it might be UTF16 but obviously I can't know what it is.

Sign up to request clarification or add additional context in comments.

Comments

1

You should specify the right encoding when reading the file. The default is UTF-8. Your file is probably encoded with a different encoding.

Comments

0

This is most likely related to the Encoding that is used when reading the file. By default, UTF8 is assumed as the Encoding. In order to read the file correctly, you need to specify the right encoding, e.g.:

string directory = @"C:\test.csv";
using(StreamReader stream = new StreamReader(directory, Encoding.ASCII))
{
    string line = "";
    line = stream.ReadLine();
    string[] column = line.Split(';');
}

You can try the following encodings (see this link for a complete list):

  • Encoding.Default for ANSI encoding based in the current windows code page.
  • Encoding.ASCII for ASCII encoding.
  • Encoding.UTF* for different Unicode encodings.

Please note that I enclosed the StreamReader in a using block so that it is disposed when it is not needed anymore.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.