473,396 Members | 1,766 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,396 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 2111
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(); ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.