使用DataContractSerializer进行序列化,但无法反序列化回来
我有以下 2 个函数:
public static string Serialize(object obj)
{
DataContractSerializer serializer = new DataContractSerializer(obj.GetType());
MemoryStream memoryStream = new MemoryStream();
serializer.WriteObject(memoryStream, obj);
return Encoding.UTF8.GetString(memoryStream.GetBuffer());
}
public static object Deserialize(string xml, Type toType)
{
MemoryStream memoryStream = new MemoryStream(Encoding.UTF8.GetBytes(xml));
// memoryStream.Position = 0L;
XmlDictionaryReader reader = XmlDictionaryReader.CreateTextReader(memoryStream, Encoding.UTF8, new XmlDictionaryReaderQuotas(), null);
DataContractSerializer dataContractSerializer = new DataContractSerializer(toType);
return dataContractSerializer.ReadObject(reader);
}
第一个函数似乎可以将对象序列化为 xml 字符串。 XML 显示有效,没有损坏的标签,开头或结尾没有空格等。现在第二个函数不想将我的 XML 字符串反序列化回对象。在最后一行我得到:
反序列化时出错 [此处是我的对象类型] 类型的对象。 根级别的数据无效。 第 1 行,位置 1。
我做错了什么?我尝试重写反序列化函数几次,但似乎总是出现同样的错误。
哦,这就是我调用这两个函数的方式:
SomeObject so = new SomeObject();
string temp = SerializationManager.Serialize(so);
so = (SomeObject)SerializationManager.Deserialize(temp, typeof(SomeObject));
I have the following 2 functions:
public static string Serialize(object obj)
{
DataContractSerializer serializer = new DataContractSerializer(obj.GetType());
MemoryStream memoryStream = new MemoryStream();
serializer.WriteObject(memoryStream, obj);
return Encoding.UTF8.GetString(memoryStream.GetBuffer());
}
public static object Deserialize(string xml, Type toType)
{
MemoryStream memoryStream = new MemoryStream(Encoding.UTF8.GetBytes(xml));
// memoryStream.Position = 0L;
XmlDictionaryReader reader = XmlDictionaryReader.CreateTextReader(memoryStream, Encoding.UTF8, new XmlDictionaryReaderQuotas(), null);
DataContractSerializer dataContractSerializer = new DataContractSerializer(toType);
return dataContractSerializer.ReadObject(reader);
}
The first one seems to serialize an object to an xml string just fine. The XML appears valid, no broken tags, no white spaces at the beginning or at the end, etc. Now the second function doesn't want to deserialize my XML string back to the object. On the last line I get:
There was an error deserializing the
object of type [MY OBJECT TYPE HERE].
The data at the root level is invalid.
Line 1, position 1.
What am I doing wrong? I tried rewriting the Deserialize function a few times, and it always seems to be the same kind of error.
Oh, and this is how I'm calling the 2 functions:
SomeObject so = new SomeObject();
string temp = SerializationManager.Serialize(so);
so = (SomeObject)SerializationManager.Deserialize(temp, typeof(SomeObject));
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我最终做了以下事情并且它有效。
看来主要问题是在调用stream.GetBuffer()时的Serialize函数中。调用stream.ToArray()似乎有效。
I ended up doing the following and it works.
It seems that the major problem was in the Serialize function when calling stream.GetBuffer(). Calling stream.ToArray() appears to work.
这是 Ray Vernagus 的稍微复杂的版本
序列化
/反序列化
方法。我使用的是StringBuilder
+XmlWriter
组合,而不是MemoryStream
+StreamReader
,它允许控制缩进XML 输出的 (XmlWriterSettings.Indent< /code>
):
使用示例:
警告:不要尝试通过转换
using
语句到using 声明,因为在读取
StringBuilder
之前必须关闭XmlWriter
。Here is a slightly more sophisticated version of Ray Vernagus's
Serialize
/Deserialize
methods. Instead of theMemoryStream
+StreamReader
I'm using theStringBuilder
+XmlWriter
combination, which allows to control the indentation of the XML output (XmlWriterSettings.Indent
):Usage example:
Caution: Don't try to improve the
using (XmlWriter
line by converting theusing
statement tousing
declaration, because theXmlWriter
has to be closed before reading theStringBuilder
.这最适合 XML 反序列化
This best for XML Deserialize
我一直都是这样做的:
Here is how I've always done it:
其他解决方案是:
备注:有时原始 xml 会包含例如:
那么你当然不能使用其他示例中使用的 UTF8 编码..
Other solution is:
One remark: sometimes it happens that raw xml contains e.g.:
<?xml version="1.0" encoding="utf-16"?>
then of course you can't use UTF8 encoding used in other examples..