473,382 Members | 1,512 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

Deserialization Error--End of Stream


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.Bin aryFormatter formatter = new System.Runtime.Serialization.Formatters.Binary.Bin aryFormatter();
try
{
formatter.Serialize(ms, ObjectToSerialize);
byte[] thisByteArray = ms.ToArray();
theSerializedObject = Convert.ToBase64String(thisByteArray);
}
catch (System.Runtime.Serialization.SerializationExcepti on 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.Bin aryFormatter formatter = new System.Runtime.Serialization.Formatters.Binary.Bin aryFormatter();

thisDeserializedObject = formatter.Deserialize(ms);
}
catch (System.Runtime.Serialization.SerializationExcepti on 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!
Nov 18 '05 #1
5 2111
Hi ,

From your description, you're using the
System.Runtime.Serialization.Formatters.Binary.Bin aryFomatter to serialize
a class object and store the output binary data into db as base64string.
However, when you retireve the data back from db and try to deserialize on
it, you get error , yes?

As for this problem, I think we can first try the following steps:
1.Try serialize the object via FileStream and store on the local file
system and deserilize from the file to see whether this work. Thus, we can
confirm whether the problem is concerned with the database or not.

2. Since you convert the serialize output into base64 string and stored in
db. What's the columns' datatype in db? Text? or varchar? or ... I
suggest that you check the byte array or base64 sttring's length before
stored into db and when retrieved back from db to see whether the length
are identical or the content has been modified.

If you have any other findings, please also feel free to post here.

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.)

Nov 18 '05 #2

Steven,

Thank you for the suggestion. I did a test

Issue myIssue = new Issue();
string IssueString = new StringFromObject(myIssue);
Issue myDeserializedIssue = new StringToObject(IssueString);

and this worked! So the issue is apparently with the data storage method. I am using ntext to store the data. Both the ntext and the Image types don't work for storing the serialized string. Do you have any suggestions? Thanks.

"Steven Cheng[MSFT]" wrote:
Hi ,

From your description, you're using the
System.Runtime.Serialization.Formatters.Binary.Bin aryFomatter to serialize
a class object and store the output binary data into db as base64string.
However, when you retireve the data back from db and try to deserialize on
it, you get error , yes?

As for this problem, I think we can first try the following steps:
1.Try serialize the object via FileStream and store on the local file
system and deserilize from the file to see whether this work. Thus, we can
confirm whether the problem is concerned with the database or not.

2. Since you convert the serialize output into base64 string and stored in
db. What's the columns' datatype in db? Text? or varchar? or ... I
suggest that you check the byte array or base64 sttring's length before
stored into db and when retrieved back from db to see whether the length
are identical or the content has been modified.

If you have any other findings, please also feel free to post here.

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.)

Nov 18 '05 #3
Solel Software wrote:
Based on your suggestion I looked into this further and was able to determine the problem. I was truncating the data by setting a limit to the stored procedure parameter:

SqlParameter parameterData = new SqlParameter("@Data", SqlDbType.NText, 16);

Once I removed the 16 it worked! Thanks for your help.


I would like to ask you how to serialize SqlParameter. When I try it,
I've error:

"The type System.Data.SqlClient.SqlParameter in Assembly System.Data,
Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 is
not marked as serializable."

What I do wrong ?
How do I have to implement this?

Thanks,
Marcin

Nov 18 '05 #4
Marcin,

I'm not sure why you are looking to serialize the SqlParameter itself. The
error you are getting is caused by the SqlParameter class not being
designated as serializable. This class is part of the .NET Framework so you
are not able to change it to be serializable (only Microsoft could do so).
However that shouldn't matter because if you are attempting to store the
serialized data in the stored procedure parameter you just have to serialize
the data into a string or binary and not the SqlParameter. For example let's
say you have a class

[Serializable()]
public class MyData
{
private int _myInt = 0;
public int myInt{ get{ return _myInt;} set{ _myInt = value;}}
}

that you would like to store in the database. Notice the [Serializable()]
attribute above the name. That tells the .NET compiler to compile the class
as serializable. You would then serialize an instance of this class
containing the data you are looking to store in the database. You would set
the SqlParameter to that serialized data. I use the following utility
methods I put together to serialize/deserialize the object to and from a
string.

private string StringFromObject(object ObjectToSerialize)
{
MemoryStream ms = new MemoryStream();
string theSerializedObject;

System.Runtime.Serialization.Formatters.Binary.Bin aryFormatter formatter
= new System.Runtime.Serialization.Formatters.Binary.Bin aryFormatter();
try
{
formatter.Serialize(ms, ObjectToSerialize);
byte[] thisByteArray = ms.ToArray();
theSerializedObject = Convert.ToBase64String(thisByteArray);
}
catch (System.Runtime.Serialization.SerializationExcepti on e)
{
Logs.WriteToLog("Error Serializing " +
ObjectToSerialize.GetType().ToString() + " Object: " + e.Message);
throw;
}
finally
{
ms.Close();
}

return theSerializedObject;
}

private object StringToObject(string StringToDeserialize)
{
object thisDeserializedObject = null;
byte[] thisByteArray = Convert.FromBase64String(StringToDeserialize);
MemoryStream ms = new MemoryStream(thisByteArray);
try
{
System.Runtime.Serialization.Formatters.Binary.Bin aryFormatter formatter
= new System.Runtime.Serialization.Formatters.Binary.Bin aryFormatter();

thisDeserializedObject = formatter.Deserialize(ms);
}
catch (System.Runtime.Serialization.SerializationExcepti on e)
{
Logs.WriteToLog("Error Deserializing " + StringToDeserialize + " String:
" + e.Message);
throw;
}
finally
{
ms.Close();
}

return thisDeserializedObject;
}

Once I've serialized the object to a string I simply set the SqlParameter to
the string. Does this answer your question?

"Marcin" wrote:
Solel Software wrote:
Based on your suggestion I looked into this further and was able to determine the problem. I was truncating the data by setting a limit to the stored procedure parameter:

SqlParameter parameterData = new SqlParameter("@Data", SqlDbType.NText, 16);

Once I removed the 16 it worked! Thanks for your help.


I would like to ask you how to serialize SqlParameter. When I try it,
I've error:

"The type System.Data.SqlClient.SqlParameter in Assembly System.Data,
Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 is
not marked as serializable."

What I do wrong ?
How do I have to implement this?

Thanks,
Marcin

Nov 18 '05 #5
> I'm not sure why you are looking to serialize the SqlParameter itself. The
error you are getting is caused by the SqlParameter class not being
designated as serializable. This class is part of the .NET Framework so you
are not able to change it to be serializable (only Microsoft could do so).
However that shouldn't matter because if you are attempting to store the
serialized data in the stored procedure parameter you just have to serialize
the data into a string or binary and not the SqlParameter. For example let's
say you have a class

[Serializable()]
public class MyData
{
private int _myInt = 0;
public int myInt{ get{ return _myInt;} set{ _myInt = value;}}
}

that you would like to store in the database. Notice the [Serializable()]
attribute above the name. That tells the .NET compiler to compile the class
as serializable. You would then serialize an instance of this class
containing the data you are looking to store in the database. You would set
the SqlParameter to that serialized data. I use the following utility
methods I put together to serialize/deserialize the object to and from a
string.

private string StringFromObject(object ObjectToSerialize)
{
MemoryStream ms = new MemoryStream();
string theSerializedObject;

System.Runtime.Serialization.Formatters.Binary.Bin aryFormatter formatter
= new System.Runtime.Serialization.Formatters.Binary.Bin aryFormatter();
try
{
formatter.Serialize(ms, ObjectToSerialize);
byte[] thisByteArray = ms.ToArray();
theSerializedObject = Convert.ToBase64String(thisByteArray);
}
catch (System.Runtime.Serialization.SerializationExcepti on e)
{
Logs.WriteToLog("Error Serializing " +
ObjectToSerialize.GetType().ToString() + " Object: " + e.Message);
throw;
}
finally
{
ms.Close();
}

return theSerializedObject;
}

private object StringToObject(string StringToDeserialize)
{
object thisDeserializedObject = null;
byte[] thisByteArray = Convert.FromBase64String(StringToDeserialize);
MemoryStream ms = new MemoryStream(thisByteArray);
try
{
System.Runtime.Serialization.Formatters.Binary.Bin aryFormatter formatter
= new System.Runtime.Serialization.Formatters.Binary.Bin aryFormatter();

thisDeserializedObject = formatter.Deserialize(ms);
}
catch (System.Runtime.Serialization.SerializationExcepti on e)
{
Logs.WriteToLog("Error Deserializing " + StringToDeserialize + " String:
" + e.Message);
throw;
}
finally
{
ms.Close();
}

return thisDeserializedObject;
}

Once I've serialized the object to a string I simply set the SqlParameter to
the string. Does this answer your question?


Yes, I was looking for method to serialize object of class with members
like SqlParameter, but now I think that it's not possible. Tough luck. I
use [NonSerialized] attribute and make it differently.

Thanks,
Marcin
Nov 18 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Tom L | last post by:
Simple deserialization help needed please... I have a packet of xml in a string, and need to get that into a reader./stream of some sort so I can properly use deserialize.. here's my...
2
by: Shone | last post by:
I would like to perform a 2-pass XML reading from a stream. Once using the Validating reader, just to confirm the validity against the schema, and next time to do a reading to extract the data....
3
by: Roy Chastain | last post by:
The following code fails with an exception of "There is an error in XML document (2, 2)." string with an inner exception of "Value cannot be null.\r\nParameter name: input" string. The inner...
1
by: BH | last post by:
I'm trying a simple object serialization and deserialization, and keep getting this error: System.Runtime.Serialization.SerializationException: Binary stream does not contain a valid...
3
by: Amadelle | last post by:
Hi all and thanks in advance for your help, I am having problems deserializing an object which seems to be serializing just fine. I save the byte array of the serialized object in the database...
3
by: AnkitAsDeveloper [Ankit] | last post by:
Hi i am serializing a 'ref struct' object as follows : private: void Seri( String ^path, Object^ obj ) { FileStream^ fileStrm ; try { //Serialize entire object into Binary stream
2
by: andreas | last post by:
Hi, if have a object arrSdList of type SortedList for serialization and deserialization there are two subs for doing this Public Sub deser Dim Formatter As BinaryFormatter = New...
3
by: parrot toes | last post by:
Summary: I have been trying to make requests of a web service provided by Axis using a dotnet client with code generated by wsdl.exe and have been getting exceptions when trying to process the...
8
by: ashoksrini | last post by:
Hi All, I have the below requirement and would like to get some feeback from the group on the best way to implement: 1. I have WSDL defined exposing few web services. 2. We dont have a...
1
by: depalau | last post by:
I'm experiencing issues where XmlSerialier.Deserialize throws an exception when attempting to use a MemoryStream built with an XmlWriter vs. a standalone StringReader. It is attempting to...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.