We help IT Professionals succeed at work.

Reading special characters from CSV with Visual Basic .NET

ablesa asked
Last Modified: 2010-04-23
I created a csv file (using excel 2007).  I need to be able to save text from different languages in this file, and I can.  I paste phrases in the csv, save it and when I open the file, the phrases show up exactly as I pasted them in.  An example of one such phrase is "Disposición".  The problem is that I need to process this csv with a visual basic program (using Visual Studio 2005).  When I read the lines of the csv into VB, the accented character shows up as a "square"...

Here's a snippet of my code:
        Dim fileLine As String

        ' open the file...
        Using streamReader As New IO.StreamReader(txtFileName.Text)

            fileLine = streamReader.ReadLine

"fileLine" contains:  "Disposiciýn"

Can someone tell me how I can read this csv and get the special characters?  I've tried creating my StreamReader like so:
Using streamReader As New IO.StreamReader(txtFileName.Text, Encoding.Unicode)

and this is worse...

Any help would be greatly appreciated!
Watch Question

Top Expert 2007
Unlock this solution and get a sample of our free trial.
(No credit card required)
Top Expert 2007

You can also use 1252, like this:

    Using streamReader As New IO.StreamReader(txtFileName.Text, Encoding.GetEncoding(1252))


the first one works with the spanish, but as I was testing further, I see that I now have a problem with my csv file.  I copied and pasted some chinese characters into my csv and saved it (using Arial).  When I pasted the text into the file, it was displaying properly, but when I save and re-open the file, it displays "????" where the chinese characters should have been.  Any ideas there?

Thanks so much for your help!
Top Expert 2007

I'm not sure how to handle that, because they use different encodings.  You may need to come up
with a way to tag each section with the appropriate codepage (ex. 1252 for Western-European), and
then extract the tag and change encoding before reading it...



no  problem.  thanks for your help.  You got me past the major hurdle!  
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.