473,763 Members | 5,412 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.F ormatters.Binar y.BinaryFormatt er
Dim MS As New IO.MemoryStream
Dim FS As New IO.FileStream(" TestFile", IO.FileMode.Cre ate)

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.Ope n)

I = DirectCast(BF.D eserialize(FS2) , Hashtable)
'This works:
MsgBox(I(3))

MS2.Write(B, 0, B.Length)
'This creates the error:
I = DirectCast(BF.D eserialize(MS2) , Hashtable)

MsgBox(I(3))

'The Error is: "End of Stream encountered before parsing was
completed."
--
--Zorpie
Nov 21 '05 #1
1 1912
On 2005-07-30, Zorpiedoman <no*********@be atles.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.F ormatters.Binar y.BinaryFormatt er
Dim MS As New IO.MemoryStream
Dim FS As New IO.FileStream(" TestFile", IO.FileMode.Cre ate)

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.Ope n)

I = DirectCast(BF.D eserialize(FS2) , Hashtable)
'This works:
MsgBox(I(3))
MS2.Write(B, 0, B.Length) '************** ************
' add this line...
MS2.Seek (0, SeekOrigin.Begi n)
'************** *************** *
'This creates the error:
I = DirectCast(BF.D eserialize(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
7737
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 ShoppingList(); myList.AddItem( new Item( "eggs",1.49 ) ); myList.AddItem( new Item( "ground beef",3.69 ) ); myList.AddItem( new Item( "bread",0.89 ) );
3
5881
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 FS As FileStream = New FileStream(FileName, FileMode.Create, FileAccess.Write, FileShare.None) IFormatter.Serialize(FS, Me) FS.Close()
4
1187
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 class of the data stored in the file. what i would like to know is: is there a way to make the function take a generic class without having to create a ton of overloaded versions of the method or using a switch statement and replicating the code?...
0
2077
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 application implementation in another .dll (let's call that app.dll). app.dll makes a function call into another dll (let's call that one dep.dll). dep.dll has some code in it where it deserializes a memorystream using a binaryformatter.
19
6246
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 read data from the transport connection." that InnerException of type System.Net.Sockets.SocketException saying "An established connection was aborted by the software in your host machine".
7
3548
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 TypeConverter other than ReferenceConverter to be put in viewstate." I've included the <Serializable()> attribute, but I'm still getting the same error. The class is below ... as you can see it contains a Collection, two
4
3572
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( CompensationPackage))).Deserialize(stream) But if I pass an XmlNodeReader to the deserializer it doesn't work:
4
4652
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 xmlns:q1="urn:CallSetup"><serviceReturn xsi:type="q1:response" xmlnssi="http://www.w3.org/2001/XMLSchema-instance">ok</serviceReturn></q1:serviceResponse>
0
2255
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(); formatter.Serialize(stream, this); }
0
9563
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9386
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
9937
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9822
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7366
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6642
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5405
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3917
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2793
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.