Problems with Serialize/Deserialize... CODE BELOW

  • Thread starter Thread starter alexandre martins
  • Start date Start date
A

alexandre martins

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
MSDN example. The CODE is BELOW this lines. If you see
something wrong or missing please answer.

Class declaration:

<Serializable()> Public Class MyO
Public n1 As Integer = 0
Public n2 As Integer = 0
Public str As String = String.Empty
End Class 'MyO


Code:

Dim objecto As New MyO()
Dim exp As New MyO()
Dim s As New System.IO.MemoryStream()
Dim formatter As New _
System.Runtime.Serialization. _
Formatters.Binary.BinaryFormatter()


objecto.n1 = 1
objecto.n2 = 2
objecto.str = "ola"

s.Capacity() = 256000
formatter.Serialize(s, objecto)

Try
exp = CType(formatter.Deserialize(s), MyO)
Catch erro As Exception
MessageBox.Show(erro.Message.ToString())
End Try
 
alexandre,
Dim exp As New MyO()
exp = CType(formatter.Deserialize(s), MyO)
I hope you realize you just "wasted" an object, if you are going to assign a
New object to a variable, you do not need to initialize the variable.

The following declares a MyO variable without initializing it.
Dim exp As MyO

I believe your problem is that formatter.Serialize leaves the stream at the
"end of file", formatter.Deserialize then attempts to read from the "end of
file". Between the Serialize & Deserialize methods you should use the
Stream.Seek to set the Stream.Position property back to the "beginning of
the file".

Hope this helps
Jay
 
Back
Top