使用DataContractSerializer来序列化,但不能反序列化

我有以下两个function:

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); } 

第一个似乎序列化一个对象到一个XMLstring就好了。 XML看起来是有效的,没有破碎的标签,在开始或结束时没有空格,等等。现在第二个函数不想反序列化我的XMLstring回到对象。 在最后一行我得到:

反序列化[MY OBJECT TYPE HERE]types的对象时发生错误。 根级别的数据是无效的。 1号线,位置1。

我究竟做错了什么? 我尝试了重写Deserialize函数几次,它总是似乎是同一种错误。 谢谢!

哦,这就是我所说的2个function:

 SomeObject so = new SomeObject(); string temp = SerializationManager.Serialize(so); so = (SomeObject)SerializationManager.Deserialize(temp, typeof(SomeObject)); 

这是我总是这样做的:

  public static string Serialize(object obj) { using(MemoryStream memoryStream = new MemoryStream()) using(StreamReader reader = new StreamReader(memoryStream)) { DataContractSerializer serializer = new DataContractSerializer(obj.GetType()); serializer.WriteObject(memoryStream, obj); memoryStream.Position = 0; return reader.ReadToEnd(); } } public static object Deserialize(string xml, Type toType) { using(Stream stream = new MemoryStream()) { byte[] data = System.Text.Encoding.UTF8.GetBytes(xml); stream.Write(data, 0, data.Length); stream.Position = 0; DataContractSerializer deserializer = new DataContractSerializer(toType); return deserializer.ReadObject(stream); } } 

我结束了以下工作。

 public static string Serialize(object obj) { using (MemoryStream memoryStream = new MemoryStream()) { DataContractSerializer serializer = new DataContractSerializer(obj.GetType()); serializer.WriteObject(memoryStream, obj); return Encoding.UTF8.GetString(memoryStream.ToArray()); } } public static object Deserialize(string xml, Type toType) { using (MemoryStream memoryStream = new MemoryStream(Encoding.UTF8.GetBytes(xml))) { XmlDictionaryReader reader = XmlDictionaryReader.CreateTextReader(memoryStream, Encoding.UTF8, new XmlDictionaryReaderQuotas(), null); DataContractSerializer serializer = new DataContractSerializer(toType); return serializer.ReadObject(reader); } } 

在调用stream.GetBuffer()时,似乎主要的问题是在Serialize函数中。 调用stream.ToArray()似乎工作。

其他解决scheme是:

 public static T Deserialize<T>(string rawXml) { using (XmlReader reader = XmlReader.Create(new StringReader(rawXml))) { DataContractSerializer formatter0 = new DataContractSerializer(typeof(T)); return (T)formatter0.ReadObject(reader); } } 

一个说法:有时会发生,原始XML包含例如:

<?xml version="1.0" encoding="utf-16"?>

那么当然你不能使用其他例子中使用的UTF8编码。

这对于XML反序列化来说是最好的

  public static object Deserialize(string xml, Type toType) { using (MemoryStream memoryStream = new MemoryStream(Encoding.UTF8.GetBytes(xml))) { System.IO.StreamReader str = new System.IO.StreamReader(memoryStream); System.Xml.Serialization.XmlSerializer xSerializer = new System.Xml.Serialization.XmlSerializer(toType); return xSerializer.Deserialize(str); } }