473,385 Members | 2,162 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,385 software developers and data experts.

Deserializing from string

I need to serialise a business object into a session as a string. The reason
for this is that we have to run session state with classic ASP and the code
we use in effect mean ASP.net sessions can only handle strings. My initial
thought is to serialize the object into a string and put it into the session
but I am having problem deserializing it.

Code to serialise
login = New login.clsLogin
Dim objStream As New MemoryStream
Dim objFormatter As New BinaryFormatter

objFormatter.Serialize(objStream, login)
Session("login") = login

Code to deserialise

Dim objFormatter As New BinaryFormatter
Dim enc As New UTF8Encoding
Dim arrBytData() As Byte = enc.GetBytes(Session("login").ToString)
Dim ms As MemoryStream = New MemoryStream()
ms.Write(arrBytData, 0, arrBytData.Length)
ms.Seek(0, 0)
login = objFormatter.Deserialize(ms)

The error I get is:

End of Stream encountered before parsing was completed.

Can anyone point me in the right direction. Regards, Chris.
Jan 22 '07 #1
2 1542
Figured it out

Dim MyFormatter As New BinaryFormatter()
Dim ms As New MemoryStream
MyFormatter.Serialize(ms, login)

Dim bytearray As Byte() = ms.ToArray()
Dim str As String = Convert.ToBase64String(bytearray)
Session("login") = str
Dim objFormatter As New BinaryFormatter
Dim GenericObject As Object
Dim arrBytData() As Byte =
Convert.FromBase64String(Session("login").ToString )
Dim ms As MemoryStream = New MemoryStream(arrBytData)

ms.Write(arrBytData, 0, arrBytData.Length)
ms.Seek(0, SeekOrigin.Begin)
GenericObject = objFormatter.Deserialize(ms)
login = CType(GenericObject, login.clsLogin)

"Chris" <no****@nospam.comwrote in message
news:uI*************@TK2MSFTNGP06.phx.gbl...
>I need to serialise a business object into a session as a string. The
reason for this is that we have to run session state with classic ASP and
the code we use in effect mean ASP.net sessions can only handle strings. My
initial thought is to serialize the object into a string and put it into
the session but I am having problem deserializing it.

Code to serialise
login = New login.clsLogin
Dim objStream As New MemoryStream
Dim objFormatter As New BinaryFormatter

objFormatter.Serialize(objStream, login)
Session("login") = login

Code to deserialise

Dim objFormatter As New BinaryFormatter
Dim enc As New UTF8Encoding
Dim arrBytData() As Byte = enc.GetBytes(Session("login").ToString)
Dim ms As MemoryStream = New MemoryStream()
ms.Write(arrBytData, 0, arrBytData.Length)
ms.Seek(0, 0)
login = objFormatter.Deserialize(ms)

The error I get is:

End of Stream encountered before parsing was completed.

Can anyone point me in the right direction. Regards, Chris.

Jan 22 '07 #2
Good, but the line:
ms.Write(arrBytData, 0, arrBytData.Length)
is unncessary, you have alreader wrapped the MemoryStream around the
arrBytData byte array and the data is already there.
Peter

--
Site: http://www.eggheadcafe.com
UnBlog: http://petesbloggerama.blogspot.com
Short urls & more: http://ittyurl.net


"Chris" wrote:
Figured it out

Dim MyFormatter As New BinaryFormatter()
Dim ms As New MemoryStream
MyFormatter.Serialize(ms, login)

Dim bytearray As Byte() = ms.ToArray()
Dim str As String = Convert.ToBase64String(bytearray)
Session("login") = str
Dim objFormatter As New BinaryFormatter
Dim GenericObject As Object
Dim arrBytData() As Byte =
Convert.FromBase64String(Session("login").ToString )
Dim ms As MemoryStream = New MemoryStream(arrBytData)

ms.Write(arrBytData, 0, arrBytData.Length)
ms.Seek(0, SeekOrigin.Begin)
GenericObject = objFormatter.Deserialize(ms)
login = CType(GenericObject, login.clsLogin)

"Chris" <no****@nospam.comwrote in message
news:uI*************@TK2MSFTNGP06.phx.gbl...
I need to serialise a business object into a session as a string. The
reason for this is that we have to run session state with classic ASP and
the code we use in effect mean ASP.net sessions can only handle strings. My
initial thought is to serialize the object into a string and put it into
the session but I am having problem deserializing it.

Code to serialise
login = New login.clsLogin
Dim objStream As New MemoryStream
Dim objFormatter As New BinaryFormatter

objFormatter.Serialize(objStream, login)
Session("login") = login

Code to deserialise

Dim objFormatter As New BinaryFormatter
Dim enc As New UTF8Encoding
Dim arrBytData() As Byte = enc.GetBytes(Session("login").ToString)
Dim ms As MemoryStream = New MemoryStream()
ms.Write(arrBytData, 0, arrBytData.Length)
ms.Seek(0, 0)
login = objFormatter.Deserialize(ms)

The error I get is:

End of Stream encountered before parsing was completed.

Can anyone point me in the right direction. Regards, Chris.


Jan 23 '07 #3

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

Similar topics

5
by: Artur Niesporek | last post by:
Hi guys, when receiving a soap response posted back from a server i get the following error when deserializing the soap message. The problem seems to be with the item-collection of the...
1
by: Thomas | last post by:
Hi, I implemented a composite pattern which should be serializable to xml. After spending some time in the newsgroups, i finally managed serializing, even with utf-8 instead of utf-16, which...
3
by: Chrigel | last post by:
Hi all, We have problems deseralizing objects previously serialized as XML. This did work fine with .NET 1.1 but since we have installed SP1, deserializing fails (but serializing works). The...
4
by: Wayne Wengert | last post by:
Using VB.NET I want to read in an XML file that has an array of objects and then step through the resulting array in code. I build a class to define the structure and I am running code to read in...
2
by: Earl Teigrob | last post by:
I am saving and restoring value types such as Int32, DateTime and Boolean in strings. I was wondering if there is a mechanism build into .NET for serializing and deserializing these to string...
2
by: Phillip Galey | last post by:
I have an object called Place which contains only string properties and has the <Serializable()> flag before the class name declaration. I also have a collection object called Places, which is...
1
by: RJN | last post by:
Hi I'm getting an error while deserializing an xml into object. System.Number.ParseDouble(String s, NumberStyles style, NumberFormatInfo info) +0 System.Double.Parse(String s, NumberStyles...
0
by: RJN | last post by:
Hi I'm getting an error while deserializing an xml into object. System.Number.ParseDouble(String s, NumberStyles style, NumberFormatInfo info) +0 System.Double.Parse(String s, NumberStyles...
1
by: =?Utf-8?B?SmVyZW15X0I=?= | last post by:
I am working on an order entry program and have a question related to deserializing nodes with nested elements. The purchase order contains multiple line items which I select using an...
2
by: =?Utf-8?B?UmF5IE1pdGNoZWxs?= | last post by:
Hi, I know this will sound like a lot of hand waving, and I'll be glad to supply some sample code if necessary, but I thought something obvious might jump out at someone without doing so. ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
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,...
0
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,...
0
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...

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.