Convert a serialized object into string

G

Guest

I have a class containing string variables, hashtables..I managed to
serialize it into a memory stream instead of file stream. Would like to know
how can I convert the byte array to string. I tried using
encoding.default.getstring but the output is an empty string..:)..damn!

partial code example that copied for microsoft for testing purpose:
Dim addresses As New Hashtable
addresses.Add("Jeff", "123 Main Street, Redmond, WA 98052")
addresses.Add("Fred", "987 Pine Road, Phila., PA 19116")
addresses.Add("Mary", "PO Box 112233, Palo Alto, CA 94301")
Dim mstrm As New MemoryStream
Dim formatter As New BinaryFormatter
formatter.Serialize(mstrm, addresses)
dim str as string = encoding.default.getstring(mstrm.toarray)

Please advise.
 
C

Cor Ligthert

Red Devil,

The nices one I know

\\\ from a message from Tom Shelton
Private Function SerializeFontObject(ByVal fnt As Font) As String
Dim bf As New BinaryFormatter
Dim mem As New MemoryStream
Try
bf.Serialize(mem, fnt)
Return Convert.ToBase64String(mem.ToArray())
Catch
Return String.Empty
Finally
mem.Close()
End Try
End Function


Private Function DeserializeFontObject(ByVal fnt As String) As Font
Dim bf As New BinaryFormatter
Dim mem As New MemoryStream(Convert.FromBase64String(fnt))
Try
Return DirectCast(bf.Deserialize(mem), Font)
Finally
If Not mem Is Nothing Then
mem.Close()
End If
End Try
End Function
///

I hope this helps a little bit?

Cor
 
G

Guest

Cor,

Another problem occurred. It doesn't happen all time thou. I did a load test
using load runner. On an occassion, the serialized string is incomplete.

Encounter such problem before?

The data is sent from client to server via socket.
 
C

Cor Ligthert

Red Devil,

That is the part I don't like.

However set a message in the newsgroup

microsoft.public.dotnet.languages.vb

There this probem is often handled. Although you can have the same results
here.

So crossposting to both newsgroups with your problem.(one message to both in
one time) will probably give you the best results.

Cor
 

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