422,949 Members | 1,026 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 422,949 IT Pros & Developers. It's quick & easy.

Writing to file mangles special characters

P: n/a
A guy called Yuri brought this up a while ago but no one got back to
him and now I have the same problem.

I have an xml file which contains special characters such as é
etc. The DTD declares these enitities and assigns them the correct
unicodes (such as é)

Now, when I've loaded the xml and converted it using the DOM and then
Console.Writelined() it, it all looks good.

Then I write it to a file thus:

FileInfo myFile = new FileInfo(filePath);
StreamWriter sw = myFile.CreateText();
sw.Write(html);
sw.Close();

When I the open the file it has replaced the characters with rubbish
such as "É".

The xml doc is utf-8 encoded (changing encoding does not appear to
make a difference)

Does anybody know what I'm missing here?
Nov 11 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
* nextman wrote in microsoft.public.dotnet.xml:
I have an xml file which contains special characters such as é
etc. The DTD declares these enitities and assigns them the correct
unicodes (such as é)

Now, when I've loaded the xml and converted it using the DOM and then
Console.Writelined() it, it all looks good.

Then I write it to a file thus:

FileInfo myFile = new FileInfo(filePath);
StreamWriter sw = myFile.CreateText();
sw.Write(html);
sw.Close();

When I the open the file it has replaced the characters with rubbish
such as "É".


That's probably the UTF-8 representation of the character but you
interprete the file as if it were in a different encoding, ISO-8859-1
or Windows-1252 for example. If you want to use a different encoding,
the .NET Framework provides means for transcoding, take a look at the
System.Text.Encoding namespace. If you are fine with UTF-8 you need
to tell your file viewer that the document is UTF-8 encoded (and ensure
that it supports UTF-8).
Nov 11 '05 #2

P: n/a
Bjoern is correct :)

Rather than doing this:

FileInfo myFile = new FileInfo(filePath);
StreamWriter sw = myFile.CreateText();
sw.Write(html);
sw.Close();

I did this:

Encoding eAnsi = System.Text.Encoding.GetEncoding(1252);
StreamWriter sw = new StreamWriter(filePath, true, eAnsi);
sw.Write( html );
sw.Close();

Thanks Bjoern for your suggestion
Nov 11 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.