'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?



Solution 1:[1]

You can specify the encoding:

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

Solution 2:[2]

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

File.WriteAllText(file, contents, encoding);

Solution 3:[3]

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

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Reginiano
Solution 2 LarsTech
Solution 3 Davide Piras