XmlWriter编码问题 [英] XmlWriter encoding issues
本文介绍了XmlWriter编码问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有以下代码:
MemoryStream ms = new MemoryStream();
XmlWriter w = XmlWriter.Create(ms);
w.WriteStartDocument(true);
w.WriteStartElement("data");
w.WriteElementString("child", "myvalue");
w.WriteEndElement();//data
w.Close();
ms.Close();
string test = UTF8Encoding.UTF8.GetString(ms.ToArray());
正确生成XML;但是,我的问题是字符串'test'的第一个字符是ï(char#239),使其对一些xml解析器无效:这是从哪里来的?我正在做什么错误?
The XML is generated correctly; however, my problem is the first character of the string 'test' is ï (char #239), making it invalid to some xml parsers: where is this coming from? What exactly am I doing incorrectly?
我知道我可以通过刚刚开始的第一个角色解决问题,但我宁愿知道为什么它在那里,而不是简单地修补
I know I can resolve the issue by just starting after the first character, but I'd rather know why it's there than simply patching over the problem.
谢谢!
推荐答案
在这里找到一个解决方案:
http://www.timvw.be/generating-utf- 8-with-systemxmlxmlwriter /
Found one solution here: http://www.timvw.be/generating-utf-8-with-systemxmlxmlwriter/
我在顶部缺少这个:
XmlWriterSettings xmlWriterSettings = new XmlWriterSettings();
xmlWriterSettings.Encoding = new UTF8Encoding(false);
MemoryStream ms = new MemoryStream();
XmlWriter w = XmlWriter.Create(ms, xmlWriterSettings);
感谢大家的帮助!
这篇关于XmlWriter编码问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文