473,378 Members | 1,475 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,378 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 6248
* 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....
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.