Using DataContractSerializer for serialization but cannot deserialize back

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 apparently serializes the object to an xml string just fine. XML seems to be valid, not broken by a tag, not white spaces at the beginning or at the end, etc. Now the second function does not want to deserialize my xml string back to the object. On the last line, I get:

Error deserializing an object of type [MY OBJECT OF TYPE HERE]. Data at the root level is invalid. Line 1, position 1.

What am I doing wrong? I tried to rewrite the Deserialize function several times, and it always seems the same error. Thank!

Oh, and I call it 2 functions:

 SomeObject so = new SomeObject(); string temp = SerializationManager.Serialize(so); so = (SomeObject)SerializationManager.Deserialize(temp, typeof(SomeObject)); 
+54
c # xml-serialization datacontractserializer
Feb 15 '11 at 10:20
source share
4 answers

Here's how I always did it:

  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); } } 
+120
Feb 15 '11 at 22:41
source share

In the end, I did the following, and it works.

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

It seems that the main problem was the Serialize function when calling stream.GetBuffer (). Calling stream.ToArray () seems to work.

+33
Feb 15 '11 at 22:53
source share

Another solution:

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

One note: sometimes it happens that raw xml contains, for example:

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

then of course you cannot use the UTF8 encoding used in other examples.

+30
Jan 02 '12 at 8:24
source share

It is the best for XML Deserialize

  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); } } 
-2
Aug 09 '11 at 12:00
source share



All Articles