473,406 Members | 2,956 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Writing to file mangles special characters

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
2 6251
* 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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Bernhard Georg Enders | last post by:
I'm using the php 'file' command to read the contents of an ASCII text file to a variable. The original text file contains some accented and special characters. The problem arises when I echo this...
7
by: Roy W. Andersen | last post by:
I've been searching google about this for days but can't find anything, so I'm hoping someone here can help me out. I'm trying to create zip-files without needing the zip-file extension in PHP,...
5
by: Matthew Shaw | last post by:
We have a web-based reporting application written in J2EE that writes out to excel using response.setContentType ("application/vnd.ms-excel; ")…. The problem is that where we have any special...
5
by: Sakharam Phapale | last post by:
Hi All, I am using an API function, which takes file path as an input. When file path contains special characters (@,#,$,%,&,^, etc), API function gives an error as "Unable to open input file"....
8
by: david.lindsay.green | last post by:
Hello all, I am quite new a web scripting and making web pages in general and I have stumbled across a problem I have as yet been unable to solve. I am trying to take the contents of a textarea box...
0
by: SeanGerman | last post by:
Howdy, Is there a way to explicitly escape special characters (particularly & (&) and ' (')) when writing a data set with the WriteXml method? I know this is supposed to happen...
7
by: Trac Bannon | last post by:
When I load XML from a file into a dotNet XMLDataDocument, the UTF-8 codes are resolved but the 5 special XML entities are not. How can I force those 5 special character types to be translated?
6
by: TheRealDan | last post by:
Hi all. I'm having a problem with a special characters. I have php script that reads from an xml file and writes to a mysql db. It's a script called phptunest that I found on the net, although the...
3
KevinADC
by: KevinADC | last post by:
Purpose The purpose of this article is to discuss the difference between characters inside a character class and outside a character class and some special characters inside a character class....
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.