Special character to &abc equivalents

C

Colin Peters

Hi,

I'm reading a file and writing it to the html output for a page.

I've come across two difficulties which I would like to solve.

The files contain special characters from European alphabets, namely
those which have the two little dots above the vowels called umlauts.

Normally, these are rendered in html using "%auml;", but in the file
they are just ä.

1. I'm using a StreamReader to read the file and I have found that if I
don't use System.Text.Encoding.UTF7 then the characters are lost
completely. Is this the correct way, or is there a way to automatically
get the Stream Reader to select the correct encoding, or use other code
to determine which would be best?

2. Having read the character from the file, it is output literally to
the html, which I guess is to be expected. Is there a way to process a
string in order to change the ä to &äuml; and so on.

Thanks in advance for any replies.
 
Y

Yunus Emre ALPÖZEN [MCAD.NET]

My advice u set underlying operating system encoding whatever u want. And
use streamreader and streamwriter with System.Text.Encoding.Default which
uses underlying OS encoding.

I had same problems with Turkish encoding but this is the best solution
(IMHO)
 
J

Joerg Jooss

Colin said:
Hi,

I'm reading a file and writing it to the html output for a page.

I've come across two difficulties which I would like to solve.

The files contain special characters from European alphabets, namely
those which have the two little dots above the vowels called umlauts.

Normally, these are rendered in html using "%auml;", but in the file
they are just ä.

1. I'm using a StreamReader to read the file and I have found that if
I don't use System.Text.Encoding.UTF7 then the characters are lost
completely.

UTF-7 is hardly what you want. Did you try ISO-8859-1? Or Windows-1252?
Is this the correct way, or is there a way to
automatically get the Stream Reader to select the correct encoding,
or use other code to determine which would be best?

In general, there's no way to guess a character encoding because
there's no universal metadata that could tell you what encoding is
being used.

To put it differently: You must know the encoding, or allow the user to
switch between possible encodings.

2. Having read the character from the file, it is output literally to
the html, which I guess is to be expected. Is there a way to process
a string in order to change the ä to &äuml; and so on.

That's not necessary if the page is encoded correctly.

Cheers,
 
C

Colin Peters

Yunus said:
My advice u set underlying operating system encoding whatever u want. And
use streamreader and streamwriter with System.Text.Encoding.Default which
uses underlying OS encoding.

I had same problems with Turkish encoding but this is the best solution
(IMHO)

Unfortunately I'm using shared hosting. I have little influence over
operating system parameters.

Thanks anyway.
 
C

Colin Peters

Joerg said:
UTF-7 is hardly what you want. Did you try ISO-8859-1? Or Windows-1252?


I didn't see this as an option provided by Intellisense for the class:
System.Text.Encoding

Thanks anyway.
 
J

Juan T. Llibre

You can set the encoding as a Page directive.

<%@Page Language="VB" ResponseEncoding="UTF-8"%>

<%@Page Language="C#" ResponseEncoding="ISO-8859-1"%>
 
J

Joerg Jooss

Colin said:
Windows-1252?


I didn't see this as an option provided by Intellisense for the class:
System.Text.Encoding

There are only a few default instances in Encoding. You can construct
all encodings by name using Encoding.GetEncoding(), e.g.

Encoding enc = Encoding.GetEncoding("ISO-8859-1").

Cheers,
 
C

Colin Peters

Aha! The penny has dropped. Or in this case, the Euro.

Many thanks to all.
 
G

Guest

Server.HtmlEncode(string) will convert any "special chars" from a text file
to the relevant &abc; equivalent without having to worry about codepages... I
use it in my chat application to prevent malicious code being inserted into
the database.

Regards,

Paul Parkinson (www.elysaria.com)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top