1

I'm using XmlTextWriter to save certain configuration elements for my program (it's only 10-15 string values, this is why I'm using XmlTextWriter). My code looks as follows:

XmlTextWriter writer = new XmlTextWriter("FILENAME.XML", null);

writer.WriteStartElement("Config");
writer.WriteElementString("Param1", param1);
writer.WriteElementString("Param2", param2);
...
writer.WriteEndElement();

writer.Close();

I would like to allow the paramX values to contain unicode. Not anything too fancy - these values comes from text-boxes the user inputs data into, and I want the system to work fine globally (Chinese, Japanese, Hebrew, Arabic, etc). I'm not parsing the data, I just want to it be presented well the next time the program loads.

What's the way to achieve this?

John Saunders
  • 160,644
  • 26
  • 247
  • 397
Bab Yogoo
  • 6,669
  • 6
  • 22
  • 17
  • FYI, you should not use `new XmlTextReader()` or `new XmlTextWriter()`. They have been deprecated since .NET 2.0. Use `XmlReader.Create()` or `XmlWriter.Create()` instead. – John Saunders Mar 11 '14 at 23:02

2 Answers2

2

The second parameter of the constructor is the encoding. The default encoding if left null is UTF8.

MyItchyChin
  • 13,733
  • 1
  • 24
  • 44
1

Well, there are two aspects here: preserving data and displaying it. XML can certainly handle Unicode, and XmlTextWriter can do so too.

What are you using to display the data though? If this is a Windows Forms application, you may need to explicitly set the font to one which can handle all the Unicode you want. It's definitely worth testing with all the character sets you're interested in (Hebrew etc).

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194