473,783 Members | 2,354 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 ObjectToSeriali ze)
{
MemoryStream ms = new MemoryStream();
string theSerializedOb ject;

System.Runtime. Serialization.F ormatters.Binar y.BinaryFormatt er formatter = new System.Runtime. Serialization.F ormatters.Binar y.BinaryFormatt er();
try
{
formatter.Seria lize(ms, ObjectToSeriali ze);
byte[] thisByteArray = ms.ToArray();
theSerializedOb ject = Convert.ToBase6 4String(thisByt eArray);
}
catch (System.Runtime .Serialization. SerializationEx ception e)
{
Logs.WriteToLog ("Error Serializing " + ObjectToSeriali ze.GetType().To String() + " Object: " + e.Message);
throw;
}
finally
{
ms.Close();
}

return theSerializedOb ject;
}
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 DeserializeObje ct(string StringToDeseria lize)
{
object thisDeserialize dObject = null;
byte[] thisByteArray = Convert.FromBas e64String(Strin gToDeserialize) ;
MemoryStream ms = new MemoryStream(th isByteArray);
try
{
System.Runtime. Serialization.F ormatters.Binar y.BinaryFormatt er formatter = new System.Runtime. Serialization.F ormatters.Binar y.BinaryFormatt er();

thisDeserialize dObject = formatter.Deser ialize(ms);
}
catch (System.Runtime .Serialization. SerializationEx ception e)
{
Logs.WriteToLog ("Error Deserializing " + StringToDeseria lize + " String: " + e.Message);
throw;
}
finally
{
ms.Close();
}

return thisDeserialize dObject;
}

The deserialized method raises an error saying "End of Stream encountered before parsing was completed." when it reaches

thisDeserialize dObject = formatter.Deser ialize(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 2140
Hi ,

From your description, you're using the
System.Runtime. Serialization.F ormatters.Binar y.BinaryFomatte r 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 StringFromObjec t(myIssue);
Issue myDeserializedI ssue = 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.F ormatters.Binar y.BinaryFomatte r 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.Sql Client.SqlParam eter in Assembly System.Data,
Version=1.0.500 0.0, Culture=neutral , PublicKeyToken= b77a5c561934e08 9 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 StringFromObjec t(object ObjectToSeriali ze)
{
MemoryStream ms = new MemoryStream();
string theSerializedOb ject;

System.Runtime. Serialization.F ormatters.Binar y.BinaryFormatt er formatter
= new System.Runtime. Serialization.F ormatters.Binar y.BinaryFormatt er();
try
{
formatter.Seria lize(ms, ObjectToSeriali ze);
byte[] thisByteArray = ms.ToArray();
theSerializedOb ject = Convert.ToBase6 4String(thisByt eArray);
}
catch (System.Runtime .Serialization. SerializationEx ception e)
{
Logs.WriteToLog ("Error Serializing " +
ObjectToSeriali ze.GetType().To String() + " Object: " + e.Message);
throw;
}
finally
{
ms.Close();
}

return theSerializedOb ject;
}

private object StringToObject( string StringToDeseria lize)
{
object thisDeserialize dObject = null;
byte[] thisByteArray = Convert.FromBas e64String(Strin gToDeserialize) ;
MemoryStream ms = new MemoryStream(th isByteArray);
try
{
System.Runtime. Serialization.F ormatters.Binar y.BinaryFormatt er formatter
= new System.Runtime. Serialization.F ormatters.Binar y.BinaryFormatt er();

thisDeserialize dObject = formatter.Deser ialize(ms);
}
catch (System.Runtime .Serialization. SerializationEx ception e)
{
Logs.WriteToLog ("Error Deserializing " + StringToDeseria lize + " String:
" + e.Message);
throw;
}
finally
{
ms.Close();
}

return thisDeserialize dObject;
}

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.Sql Client.SqlParam eter in Assembly System.Data,
Version=1.0.500 0.0, Culture=neutral , PublicKeyToken= b77a5c561934e08 9 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 StringFromObjec t(object ObjectToSeriali ze)
{
MemoryStream ms = new MemoryStream();
string theSerializedOb ject;

System.Runtime. Serialization.F ormatters.Binar y.BinaryFormatt er formatter
= new System.Runtime. Serialization.F ormatters.Binar y.BinaryFormatt er();
try
{
formatter.Seria lize(ms, ObjectToSeriali ze);
byte[] thisByteArray = ms.ToArray();
theSerializedOb ject = Convert.ToBase6 4String(thisByt eArray);
}
catch (System.Runtime .Serialization. SerializationEx ception e)
{
Logs.WriteToLog ("Error Serializing " +
ObjectToSeriali ze.GetType().To String() + " Object: " + e.Message);
throw;
}
finally
{
ms.Close();
}

return theSerializedOb ject;
}

private object StringToObject( string StringToDeseria lize)
{
object thisDeserialize dObject = null;
byte[] thisByteArray = Convert.FromBas e64String(Strin gToDeserialize) ;
MemoryStream ms = new MemoryStream(th isByteArray);
try
{
System.Runtime. Serialization.F ormatters.Binar y.BinaryFormatt er formatter
= new System.Runtime. Serialization.F ormatters.Binar y.BinaryFormatt er();

thisDeserialize dObject = formatter.Deser ialize(ms);
}
catch (System.Runtime .Serialization. SerializationEx ception e)
{
Logs.WriteToLog ("Error Deserializing " + StringToDeseria lize + " String:
" + e.Message);
throw;
}
finally
{
ms.Close();
}

return thisDeserialize dObject;
}

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
2084
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 deserialization routine (its part of a property as you can tell).. Set(ByVal Value As String) Dim serializer As New XmlSerializer(GetType(Package)) Dim xmlstream As New MemoryStream
2
9452
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. Actually, second time I do a deserialization, the data from XML is fed directly to an object. The problem I am experiencing is the error at the second reading attempt, and error description implies that reader is winded to the end of the XML and...
3
2082
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 exception has a strack trace of " at System.Text.RegularExpressions.Regex.Split(String input, Int32 count)\r\n at KMS.Licensing.LicenseBase.Parse() in c:\\kms\\licensing\\common\\licensebase.cs:line 672\r\n at...
1
12063
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 BinaryHeader, 0 possible causes, invalid stream or object version change between serialization and deserialization. Here's my code. it does nothing but to serialize a DataTable object into a byte array, and then read the byte array back for...
3
9796
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 (when I check the database the field that holds the binary data seems populated), but when I want to deserialize this same byte array the application fails and gives me the following error: Binary stream does not contain a valid BinaryHeader, 0...
3
2069
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
1775
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 BinaryFormatter() Dim ReadFile As FileStream ReadFile = File.OpenRead(FilePad)
3
9795
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 response. As a result of seraching news groups I guessed that the SOAP response defines an array element in a way that causes the dotnet deserialization routines to put the content in a generic object array (object) BUT the content is supposed to...
8
3470
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 requirement to have a server web service class. (reasons below) 3. I want to develop something like this - when client makes a web service call, on the server I can intercept the SOAP message (XML doc itself),
1
4951
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 deserialize a complex object. Here is what I have: 0. Environment: Visual Studio 2005, VB.Net. I won't go into the gory details of my complex object, just suffice to say that I need to return XML instead of the object directly from the web service....
0
9643
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10313
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10147
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
7494
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6735
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5378
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4044
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3643
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2875
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.