473,287 Members | 1,865 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,287 software developers and data experts.

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 2106
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Carl Mercier | last post by:
Hi! I have 2 different applications/assembly. The first one creates an object and serializes it to a textfile on disk. The second one has the the exact same class (copied/pasted). It reads...
3
by: Amsteel | last post by:
I got something like this in VB.Net <Serializable()> Public Class YQProfileC Inherits CollectionBase Public Sub Save2File(ByVal FileName As String) Dim IFormatter As New BinaryFormatter() Dim...
2
by: PCH | last post by:
I have 2 functions, one to serialize an object, and one to deserialize it. I can serialize just fine, the problem is when I try to deserialize it later... I get an error: {"Invalid...
2
by: alexandre martins | last post by:
Every time i try to make Deserialize the computer gives me the folowing error: "End of Stream encountered before parsing was complete" the code that i'm running is simple and is based on an...
4
by: Samuel R. Neff | last post by:
I'm deserializing an XML file. If I pass a Stream to the file directly to the deserializer as follows it works fine: o = (New XmlSerializer( GetType(...
2
by: Thomas S | last post by:
Any suggestions on how to deserialize an object from one line of XML? I'm trying to deserialize multiple objects from one XML document, each object on one line of the file. The serialization is...
1
by: Heron | last post by:
Hi, I'm having a problem deserializing my streams since they are utf8 encoded (they are being received over tcp/ip) so I was looking for a way to make the serializer use utf8, is there any? ...
11
by: wpmccormick | last post by:
I've a complex problem: I'm deserializing a very long string of XML into a very large object foo: <foo> ....... <bar>sometimes a simple string is here</bar> ....... </foo>
0
by: connectpalm03-forum | last post by:
I have a class named (MyClassA) in ControlClasses.dll and was able to serialize it to database. Like below SaveTo(MemoryStream stream) { IFormatter formatter = new BinaryFormatter(); ...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...

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.