2011-02-15 88 views
54

我有以下两个功能:使用DataContractSerializer的序列化,但不能反序列化回

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字符串反序列化回对象。在我得到的最后一行:

反序列化类型为[MY OBJECT TYPE HERE]的 对象时出错。 根级别的数据无效。 Line 1,position 1.

我在做什么错?我尝试了几次重写Deserialize函数,并且它似乎总是出现同样的错误。谢谢!

哦,这是我如何调用两个功能:

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

回答

121

这里是我怎么总是做它:

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

对于反序列化,将GetBytes/Write替换为“使用(StreamWriter writer = new StreamWriter(stream,encoding){writer.Write(xml); ...}“以避免额外的字节[]缓冲区 – crokusek 2013-01-10 03:51:00

+1

这真棒,并且仍然非常有帮助,最重要的是,这是正确的 – Feign 2015-07-27 18:24:40

+0

我也使用过这些方法,但是当serialize-deserialize ([link](https://stackoverflow.com/a/45443874/3785903))来提供基类的类型。 – 2017-08-01 17:15:38

33

最后我做以下,并作品。

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()似乎工作。

+1

+1对于你的基类,我需要修改serialize-method([link]解决方案 – Divi 2011-02-15 22:56:04

-2

这最好的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); 
     } 

    } 
31

其他的解决方案是:

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编码在其他例子中使用..

相关问题