10

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?

3
  • This should not be a problem because by default WriteAllText() is using UTF8 encoding - sure you don't have a problem elsewhere? Please post some code Commented Aug 5, 2011 at 15:38
  • @BrokenGlass you might be right - it could also be XElement.ToString() ... is this the right way to get the unicode string from an xelement? Commented Aug 5, 2011 at 16:10
  • @BrokenGlass Not true. I had the same problem and added Encoding.UTF8 as 3rd parameter and now Unicode characters are not lost while saving with WriteAllText. Commented Dec 23, 2011 at 12:07

4 Answers 4

16

You can specify the encoding:

File.WriteAllText(fileName, xml, Encoding.Unicode); 
Sign up to request clarification or add additional context in comments.

Comments

6

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

Comments

2

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

File.WriteAllText(file, contents, encoding); 

Comments

2

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.