470,632 Members | 2,082 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,632 developers. It's quick & easy.

Deserialize problem

I have 2 solution to deserialize ArrayList which contains objects
I am using for this MemoryStream, but MemoryStream is not functional - I
receive always error
#End of Stream encountered before parsing was completed.#

The second solution is functionall - I am using in this solution
FileStream - everything works fine !!
I want to use MemoryStream instead of FileStrem
Can somebody tells me where I have problem ?
I am using the same source: byte array Dim bytes() As Byte
Thanks
#################### First solution with ERROR ############

Dim bytes() As Byte = row("PANEL_DATA") 'DataRow, column PANEL_DATA

Dim bFormatter As New BinaryFormatter()
Dim ms As MemoryStream
Dim b As Byte
ms = New MemoryStream()
ms.SetLength(bytes.Length)
Dim i As Int32
For i = 0 To bytes.Length - 1
ms.WriteByte(bytes(i))
Next

Try
########### in this line is error ###############
Me.objList = bFormatter.Deserialize(ms)
Catch ex As Exception
ms.Close()
gShowError(ex, "LoadControlsSer - deserialize")
End Try
ms.Close()

#################### Second solution - working solution ############

Dim ms As New MemoryStream()
Dim bFormatter As New BinaryFormatter()
ms.Write(bytes, 0, bytes.Length)
Dim stream11 As Stream = New FileStream("C:\MyFile.bin", FileMode.Create,
FileAccess.Write, FileShare.None)
stream11.Write(bytes, 0, bytes.Length)
stream11.Close()
Try
Dim formatter As BinaryFormatter = New BinaryFormatter()
Dim stream22 As Stream = New FileStream("C:\MyFile.bin", FileMode.Open,
FileAccess.Read, FileShare.Read)
Me.objList = formatter.Deserialize(stream22)
stream22.Close()
Catch ex As Exception
gShowError(ex, "LoadControlsSer")
Return False
End Try

Return True
Tomas Oplt

Tel: 603 171893

Nov 20 '05 #1
1 1959
In article <e5**************@TK2MSFTNGP12.phx.gbl>, Pavel Pavel wrote:
I have 2 solution to deserialize ArrayList which contains objects
I am using for this MemoryStream, but MemoryStream is not functional - I
receive always error
#End of Stream encountered before parsing was completed.#

The second solution is functionall - I am using in this solution
FileStream - everything works fine !!
I want to use MemoryStream instead of FileStrem
Can somebody tells me where I have problem ?
I am using the same source: byte array Dim bytes() As Byte
Thanks
#################### First solution with ERROR ############

Dim bytes() As Byte = row("PANEL_DATA") 'DataRow, column PANEL_DATA

Dim bFormatter As New BinaryFormatter()
Dim ms As MemoryStream
Dim b As Byte
ms = New MemoryStream()
ms.SetLength(bytes.Length)
Dim i As Int32
For i = 0 To bytes.Length - 1
ms.WriteByte(bytes(i))
Next


You could do away with the above... You can create a memory stream from
the byte array directly :)

Dim bytes() As Byte = row("PANEL_DATA")
Dim bFormater As New BinaryFormatter()
Dim ms As New MemoryStream(ms)

Try
Me.objList = bFormatter.Deserialize(ms)
Catch ex As Exception
gShowError(ex, "LoadControlsSer - deserialize")
Finally
ms.Close()
End Try
That should work :) In your old solution, though - you need to add this
line right after the loop.

ms.Seek(0, SeekOrigin.Begin)

ms.WriteByte was moving the current pointer down the stream, so when you
call deserialize - the pointer is at the end of the stream.
--
Tom Shelton
MVP [Visual Basic]
Nov 20 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Carl Mercier | last post: by
3 posts views Thread by Amsteel | last post: by
2 posts views Thread by PCH | last post: by
2 posts views Thread by alexandre martins | last post: by
2 posts views Thread by Thomas S | last post: by
1 post views Thread by Heron | last post: by
11 posts views Thread by wpmccormick | last post: by
reply views Thread by connectpalm03-forum | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.