Deserialization Error--End Of Stream

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!
 
S

Steven Cheng[MSFT]

Hi,

As for this post. I've replied you in another duplicated one in this group.
I'd appreciate if you have a look there. Also, if you feel convenient that
we continue to discuss in that thread, please feel free to follow up there
..Thanks.

Thanks.

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,969
Messages
2,570,161
Members
46,710
Latest member
bernietqt

Latest Threads

Top