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));
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));
Dimskiy Feb 15 '11 at 10:20 2011-02-15 22:20
source share