G
Guest
Hello,
I am attempting to serialize an object for storage in a DB field and them deserialize it later on. I have managed to Serialize it using
private string SerializeObject(object ObjectToSerialize)
{
MemoryStream ms = new MemoryStream();
string theSerializedObject;
System.Runtime.Serialization.Formatters.Binary.BinaryFormatter formatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
try
{
formatter.Serialize(ms, ObjectToSerialize);
byte[] thisByteArray = ms.ToArray();
theSerializedObject = Convert.ToBase64String(thisByteArray);
}
catch (System.Runtime.Serialization.SerializationException e)
{
Logs.WriteToLog("Error Serializing " + ObjectToSerialize.GetType().ToString() + " Object: " + e.Message);
throw;
}
finally
{
ms.Close();
}
return theSerializedObject;
}
I store the string returned by the above method in the DB. But I am having to trouble Deserializing it. I have the following method:
private object DeserializeObject(string StringToDeserialize)
{
object thisDeserializedObject = null;
byte[] thisByteArray = Convert.FromBase64String(StringToDeserialize);
MemoryStream ms = new MemoryStream(thisByteArray);
try
{
System.Runtime.Serialization.Formatters.Binary.BinaryFormatter formatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
thisDeserializedObject = formatter.Deserialize(ms);
}
catch (System.Runtime.Serialization.SerializationException e)
{
Logs.WriteToLog("Error Deserializing " + StringToDeserialize + " String: " + e.Message);
throw;
}
finally
{
ms.Close();
}
return thisDeserializedObject;
}
The deserialized method raises an error saying "End of Stream encountered before parsing was completed." when it reaches
thisDeserializedObject = formatter.Deserialize(ms);
What might be causing this? I could provide some sample data if it would help. Thanks for your help!
I am attempting to serialize an object for storage in a DB field and them deserialize it later on. I have managed to Serialize it using
private string SerializeObject(object ObjectToSerialize)
{
MemoryStream ms = new MemoryStream();
string theSerializedObject;
System.Runtime.Serialization.Formatters.Binary.BinaryFormatter formatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
try
{
formatter.Serialize(ms, ObjectToSerialize);
byte[] thisByteArray = ms.ToArray();
theSerializedObject = Convert.ToBase64String(thisByteArray);
}
catch (System.Runtime.Serialization.SerializationException e)
{
Logs.WriteToLog("Error Serializing " + ObjectToSerialize.GetType().ToString() + " Object: " + e.Message);
throw;
}
finally
{
ms.Close();
}
return theSerializedObject;
}
I store the string returned by the above method in the DB. But I am having to trouble Deserializing it. I have the following method:
private object DeserializeObject(string StringToDeserialize)
{
object thisDeserializedObject = null;
byte[] thisByteArray = Convert.FromBase64String(StringToDeserialize);
MemoryStream ms = new MemoryStream(thisByteArray);
try
{
System.Runtime.Serialization.Formatters.Binary.BinaryFormatter formatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
thisDeserializedObject = formatter.Deserialize(ms);
}
catch (System.Runtime.Serialization.SerializationException e)
{
Logs.WriteToLog("Error Deserializing " + StringToDeserialize + " String: " + e.Message);
throw;
}
finally
{
ms.Close();
}
return thisDeserializedObject;
}
The deserialized method raises an error saying "End of Stream encountered before parsing was completed." when it reaches
thisDeserializedObject = formatter.Deserialize(ms);
What might be causing this? I could provide some sample data if it would help. Thanks for your help!