XmlTextWriter序列化问题 [英] XmlTextWriter serialization problem

查看:70
本文介绍了XmlTextWriter序列化问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个xml.我已经使用xsd.exe创建了数据类.根类是 MESSAGE .

I'm trying to create a piece of xml. I've created the dataclasses with xsd.exe. The root class is MESSAGE.

因此,在创建 MESSAGE 并填充其所有属性后,我将其序列化如下:

So after creating a MESSAGE and filling all its properties, I serialize it like this:

serializer = new XmlSerializer(typeof(Xsd.MESSAGE));
StringWriter sw = new StringWriter();
serializer.Serialize(sw, response);
string xml = sw.ToString();

到目前为止,一切顺利,字符串xml包含有效(UTF-16编码)的xml.现在,我想改为使用UTF-8编码创建xml,所以我这样做是这样的:

Up until now all goes well, the string xml contains valid (UTF-16 encoded) xml. Now I like to create the xml with UTF-8 encoding instead, so I do it like this:

编辑:忘记包含流的声明

serializer = new XmlSerializer(typeof(Xsd.MESSAGE));
using (MemoryStream stream = new MemoryStream())
{
    XmlTextWriter xtw = new XmlTextWriter(stream, Encoding.UTF8);
    serializer.Serialize(xtw, response);
    string xml = Encoding.UTF8.GetString(stream.ToArray());
}

问题就来了:使用这种方法,xml字符串前面有一个无效的char(臭名昭著的正方形).
当我这样检查字符时:

And here comes the problem: Using this approach, the xml string is prepended with an invalid char (the infamous square).
When I inspect the char like this:

char c = xml[0];

我可以看到c的值为65279.
有人知道这是从哪里来的吗?
我可以通过截断第一个字符来轻松解决此问题:

I can see that c has a value of 65279.
Anybody has a clue where this is coming from?
I can easily solve this by cutting off the first char:

xml = xml.SubString(1);

但是我宁愿知道发生了什么,也不要盲目地切割第一个字符.

But I'd rather know what's going on than blindly cutting of the first char.

任何人都可以对此有所了解吗?谢谢!

Anybody can shed some light on this? Thanks!

推荐答案

在这里,您的代码已修改为不添加字节顺序标记(

Here's your code modified to not prepend the byte-order-mark (BOM):

var serializer = new XmlSerializer(typeof(Xsd.MESSAGE));
Encoding utf8EncodingWithNoByteOrderMark = new UTF8Encoding(false);
XmlTextWriter xtw = new XmlTextWriter(stream, utf8EncodingWithNoByteOrderMark);
serializer.Serialize(xtw, response);
string xml = Encoding.UTF8.GetString(stream.ToArray());

这篇关于XmlTextWriter序列化问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆