Solved

Reading German special characters using a streamreader with currentEncoding = system.text.utf8encoding

Posted on 2007-11-17
4
1,488 Views
Last Modified: 2010-04-21
I'm using a streamreader to read a text file.

The streamreader.currentencoding is system.text.utf8encoding

The first line of my test text file is the following string of characters:
Uppercase A with an umlaut (HTML code would be Ä)
Lowercase a with an umlaut (ä)
Uppercase O with an umlaut (Ö)
Lowercase o with an umlaut (ö)
Uppercase U with an umlaut (Ü)
Lowercase u with an umlaut (ü)
The German lowercase "SZ" symbol (ß)

This is interpreted as a series of unknown characters.

What can I do to make the stream reader interpret the text correctly.

Thanks very much in advance,

JaimeHy
0
Comment
Question by:jaimehy
  • 2
  • 2
4 Comments
 
LVL 29

Accepted Solution

by:
Göran Andersson earned 500 total points
Comment Utility
Then the file is obviously not encoded  using UTF-8. You have to specify the encoding that was used to create the text file.

If it's a ANSI (Windows pre-unicode) text file, the Encoding.Default property will give you the current ANSI encoding in the system, which will likely match the encoding used to create the file.

If the file is unicode, but not UTF-8, it usually has a BOM (byte order mark) at the beginning of the file. The StreamReader has a constructor that takes a boolean argument named detectEncodingFromByteOrderMarks that you can use to let the StreamReader select encoding based on the BOM.

If that fails, you can read the file using a FileStream, and examine the actual byte values in the file to see if there is any BOM, and what character codes you get for the special characters.
0
 
LVL 29

Expert Comment

by:Göran Andersson
Comment Utility
As an alternative to using a FileStream to examine the file, just add a .bin file extension to the file and open it in Visual Studio.
0
 

Author Closing Comment

by:jaimehy
Comment Utility
It gave me the pointers I needed to resolve the problem I had.  More than that I couldn't ask for.
0
 

Author Comment

by:jaimehy
Comment Utility
Thanks for the tips, GreenGhost,

Yes, I did forget to mention that the txt file is encoded in ANSI.  Sorry about that.

The question that I suppose I wanted to ask is how to force the streamreader to read the file as ANSI.  With your help,I've managed to answer it.

The StreamReader.CurrentEncoding property is read only, so no luck there, and I was stuck.   What I eventually did was declare a default encoding and overload the arguments when declaring my streamreader as follows:

        Dim DefaultEncoding As Encoding = Encoding.Default
        Dim sr As New StreamReader(FileToBeRead, DefaultEncoding)

My umlauts now appear perfectly in my output xml file!

Much obliged


JaimeHY
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now