cs0815
cs0815

Reputation: 17388

Problem using unicode in File.WriteAllText

I have some raw data (xml) which I definitely receive containing unicode. I write them to a file using:

File.WriteAllText

This seems to remove/change unicode characters. Is there a way to prevent this?

Upvotes: 9

Views: 14172

Answers (4)

Mark Cidade
Mark Cidade

Reputation: 99957

You can specify the encoding:

File.WriteAllText(fileName, xml, Encoding.Unicode);

Upvotes: 15

Justin
Justin

Reputation: 86729

Try the File.WriteAllText overload which allows you to specify an encoding - just give it the same encoding of the original data.

Upvotes: 6

Davide Piras
Davide Piras

Reputation: 44605

you can specify the Encoding as parameter for the WriteAllText function, see the available overloads :)

Upvotes: 2

LarsTech
LarsTech

Reputation: 81610

Use the proper encoding, which is the 3rd parameter.

File.WriteAllText(file, contents, encoding);

Upvotes: 2

Related Questions