XmlTextWriter:允许 Unicode?
我使用 XmlTextWriter 为我的程序保存某些配置元素(它只有 10-15 个字符串值,这就是我使用 XmlTextWriter 的原因)。 我的代码如下所示:
XmlTextWriter writer = new XmlTextWriter("FILENAME.XML", null);
writer.WriteStartElement("Config");
writer.WriteElementString("Param1", param1);
writer.WriteElementString("Param2", param2);
...
writer.WriteEndElement();
writer.Close();
我想允许 paramX 值包含 unicode。 没什么太奇特的——这些值来自用户输入数据的文本框,我希望系统在全球范围内都能正常工作(中文、日语、希伯来语、阿拉伯语等)。 我不是在解析数据,我只是希望在下次程序加载时能够很好地呈现数据。
有什么方法可以达到这个目的呢?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
构造函数的第二个参数是 编码。 如果留空,则默认编码为 UTF8。
The second parameter of the constructor is the encoding. The default encoding if left null is UTF8.
嗯,这里有两个方面:保存数据和显示数据。 XML 当然可以处理 Unicode,
XmlTextWriter
也可以这样做。你用什么来显示数据? 如果这是 Windows 窗体应用程序,您可能需要将字体显式设置为可以处理您想要的所有 Unicode 的字体。 绝对值得用您感兴趣的所有字符集(希伯来语等)进行测试。
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).