473,320 Members | 2,133 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,320 software developers and data experts.

Can't Deserialize a Hashtable using a Memory Stream...

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.Bin aryFormatter
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."
--
--Zorpie
Nov 21 '05 #1
1 1876
On 2005-07-30, Zorpiedoman <no*********@beatles.com> wrote:
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.Bin aryFormatter
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) '**************************
' add this line...
MS2.Seek (0, SeekOrigin.Begin)
'******************************
'This creates the error:
I = DirectCast(BF.Deserialize(MS2), Hashtable)

MsgBox(I(3))

'The Error is: "End of Stream encountered before parsing was
completed."


You need to move the position pointer back to the begining of the stream
:)
--
Tom Shelton [MVP]
Nov 21 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Mullin Yu | last post by:
i want to deserialize a object to a xml string. what i currently have is deserialize it to a xml file. i don't want to have one more step to load it back!! ShoppingList myList = new...
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...
4
by: Shawn | last post by:
Hi All; i have created a class that handles file input and output. one of the methods loads data from a file then returns a hashtable filled with data class. it takes in the file name and the...
0
by: Fruber Malcome | last post by:
I'm getting a very weird exception and hoping someone may be able to help. I have an Office Add-In that lives in a .dll (for email reference ai.dll) ai.dll makes calls into the core part of the...
19
by: Sharon | last post by:
Hi, When I'm doing BinaryFormatter.Deserialize() over a TCP socket. When I'm closing the TcpListener by invoking the TcpListener.Stop(); I get: System.IO.IOException with message "Unable to...
7
by: Ben Amada | last post by:
I've created a class that I need to store in ViewState. However when I try to store it in ViewState, I get the following error: "The type 'solution.pe2' must be marked as Serializable or have a...
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(...
4
by: Anbu | last post by:
Hi All, I need to Deserialize the SoapService's SoapEnvelope response object. Here is the Body of the SoapEnvelope received as respone, <q1:serviceResponse...
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:
ExcelToDatabase: batch import excel into database automatically...
1
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: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
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)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.