469,643 Members | 1,910 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

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

Problems with Serialize/Deserialize... CODE BELOW

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
Nov 20 '05 #1
2 2268
"alexandre martins" <an*******@discussions.microsoft.com> wrote...
s.Capacity() = 256000
formatter.Serialize(s, objecto)
I'll guess that you need to rewind the stream...

s.Position = 0

Try
exp = CType(formatter.Deserialize(s), MyO)
Catch erro As Exception
MessageBox.Show(erro.Message.ToString())
End Try

Nov 20 '05 #2
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
"alexandre martins" <an*******@discussions.microsoft.com> wrote in message
news:06****************************@phx.gbl... 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

Nov 20 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Wayne Wengert | last post: by
14 posts views Thread by vince | last post: by
5 posts views Thread by David Sworder | last post: by
4 posts views Thread by =?Utf-8?B?Qnlyb24=?= | last post: by
reply views Thread by gheharukoh7 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.