Serialize Object then Unserialize

D

Derek Martin

Hey list, I have an arraylist containing some objects that I want to
serialize and send over the internet and then deserialize back into the
arraylist of objects.

What I have so far:
Dim myfeeds As New ArrayList
myfeeds = <<method that populates arraylist with objects>>

Dim BinFormatter As New
System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
Dim memStream As New System.IO.MemoryStream
BinFormatter.Serialize(memStream, myfeeds)
memStream.Position = 0
Dim stremRead As New System.IO.StreamReader(memStream)
Dim oEncoder As New System.Text.ASCIIEncoding
Dim thisbyte As Byte() = oEncoder.GetBytes(stremRead.ReadToEnd)
Dim thisbase64 As String = Convert.ToBase64String(thisbyte)

then, send thisbase64 string across the net...so far so good?

Now, I need to take this back into arraylist, and am probably just having a
brain fart but am unable to get this order properly reversed:

Dim bytes2 As Byte() = Convert.FromBase64String(thisbase64)
Dim memstream2 As New System.IO.MemoryStream(bytes2)
Dim stremread2 As New System.IO.StreamWriter(memstream2)
Dim mysecondfeeds As ArrayList =
CType(BinFormatter.Deserialize(memstream2), ArrayList)....causes format
change exception
annnnnd.....brain fart

Anyone can help???

Thanks,
Derek
 
C

Cor Ligthert [MVP]

Derek,

I hope you don't mind I did not look at your code.

This sample should work
\\\
Private Function SerializeArraylist(ByVal _
arraylst As ArrayList) As String
Dim bf As New
Runtime.Serialization.Formatters.Binary.BinaryFormatter
Dim mem As New IO.MemoryStream
bf.Serialize(mem, arraylst)
Return Convert.ToBase64String(mem.ToArray())
End Function
Private Function DeserializeArraylist(ByVal _
arraystring As String) As ArrayList
Dim bf As New
Runtime.Serialization.Formatters.Binary.BinaryFormatter
Dim mem As New
IO.MemoryStream(Convert.FromBase64String(arraystring))
Return DirectCast(bf.Deserialize(mem), ArrayList)
End Function
///

I hope this helps,

Cor
 
D

Derek Martin

Cor, as always, you are the bomb dot com - just what I was looking for and
saved me a few lines of code to boot!

Derek
 

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

Top