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”的第一个字符是ï(字符#239),这使得它对某些xml解析器无效:这是从哪里来的? 我到底做错了什么?
我知道我可以通过在第一个字符之后开始来解决问题,但我宁愿知道为什么会出现这个问题,而不是简单地修补问题。
谢谢!
I have the following code:
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());
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.
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
在这里找到了一种解决方案:
https://timvw.be/2007/01/08 /generate-utf-8-with-systemxmlxmlwriter/
我在顶部遗漏了这一点:
感谢大家的帮助!
Found one solution here:
https://timvw.be/2007/01/08/generating-utf-8-with-systemxmlxmlwriter/
I was missing this at the top:
Thanks for the help everyone!
问题在于,编写器生成的 XML 是 UTF-16,而您使用 UTF-8 将其转换为字符串。 试试这个:
The problem is that your the XML generated by the writer is UTF-16 while you use UTF-8 to convert it to string. Try this instead:
您可以像这样更改编码:
You can change encodings like this:
如果您关心编辑器使用的字节顺序标记(例如 Visual Studio 检测 UTF8 编码的 XML 并正确突出显示语法),那么所有这些都略有偏差。
这是一个解决方案:
我有两个完整的片段 这里
All of these are slightly off, if you care about the byte order mark which is something editors use (such as Visual Studio detecting UTF8 encoded XML and syntax highlighting properly).
Here's a solution:
I've got 2 snippets in full here