By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,725 Members | 1,094 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,725 IT Pros & Developers. It's quick & easy.

Serialize Object then Unserialize

P: n/a
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.Bin aryFormatter
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
Nov 21 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
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.BinaryForm atter
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.BinaryForm atter
Dim mem As New
IO.MemoryStream(Convert.FromBase64String(arraystri ng))
Return DirectCast(bf.Deserialize(mem), ArrayList)
End Function
///

I hope this helps,

Cor
Nov 21 '05 #2

P: n/a
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


"Cor Ligthert [MVP]" <no************@planet.nl> wrote in message
news:ew**************@tk2msftngp13.phx.gbl...
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.BinaryForm atter
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.BinaryForm atter
Dim mem As New
IO.MemoryStream(Convert.FromBase64String(arraystri ng))
Return DirectCast(bf.Deserialize(mem), ArrayList)
End Function
///

I hope this helps,

Cor

Nov 21 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.