G
Guest
The following code explains my problem. It works fine if I use a file
stream, but not if I use a memory stream. What's the problem here? It must
be something simple I am missing...
Dim H As New Hashtable
H.Add(1, "First Item")
H.Add(2, "Second Item")
H.Add(3, "Third Item")
Dim BF As New
System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
Dim MS As New IO.MemoryStream
Dim FS As New IO.FileStream("TestFile", IO.FileMode.Create)
BF.Serialize(MS, H)
BF.Serialize(FS, H)
Dim B As Byte() = MS.ToArray
MS.Close()
FS.Close()
'----------------- So Far So Good, now I attempt to De-Serialize
Dim I As New Hashtable
Dim MS2 As New IO.MemoryStream
Dim FS2 As New IO.FileStream("TestFile", IO.FileMode.Open)
I = DirectCast(BF.Deserialize(FS2), Hashtable)
'This works:
MsgBox(I(3))
MS2.Write(B, 0, B.Length)
'This creates the error:
I = DirectCast(BF.Deserialize(MS2), Hashtable)
MsgBox(I(3))
'The Error is: "End of Stream encountered before parsing was
completed."
stream, but not if I use a memory stream. What's the problem here? It must
be something simple I am missing...
Dim H As New Hashtable
H.Add(1, "First Item")
H.Add(2, "Second Item")
H.Add(3, "Third Item")
Dim BF As New
System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
Dim MS As New IO.MemoryStream
Dim FS As New IO.FileStream("TestFile", IO.FileMode.Create)
BF.Serialize(MS, H)
BF.Serialize(FS, H)
Dim B As Byte() = MS.ToArray
MS.Close()
FS.Close()
'----------------- So Far So Good, now I attempt to De-Serialize
Dim I As New Hashtable
Dim MS2 As New IO.MemoryStream
Dim FS2 As New IO.FileStream("TestFile", IO.FileMode.Open)
I = DirectCast(BF.Deserialize(FS2), Hashtable)
'This works:
MsgBox(I(3))
MS2.Write(B, 0, B.Length)
'This creates the error:
I = DirectCast(BF.Deserialize(MS2), Hashtable)
MsgBox(I(3))
'The Error is: "End of Stream encountered before parsing was
completed."