473,386 Members | 1,819 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,386 software developers and data experts.

MemoryStream deserialized

M D
Please tell me why this:

static void Main(string[] args) {
Bid bid1 = new Bid(23,10.0F,3,0,DateTime.Now);
BinaryFormatter formatter = new BinaryFormatter();
Stream depot = new MemoryStream();
formatter.Serialize(depot, bid1);
Console.WriteLine("size of a bid- {0}", depot.Length);
Console.WriteLine("bid1 {0}", bid1);
Console.WriteLine(depot.CanRead);
Bid bid2 = (Bid)formatter.Deserialize(depot);
Console.WriteLine("bid2 {0}", bid2);
depot.Close();
}

results in this:

...\bin\Debug>consoleapplication1
size of a bid- 226
bid1 23:10:3:0:052405160487
True

Unhandled Exception:
System.Runtime.Serialization.SerializationExceptio n: End of
Stream encountered before parsing was completed.

Bid is:
struct Bid {
int p;
float o;
int b;
short w;
string tS;

which, in a constructor, converts the DateTime to a formatted string.

I've tried commenting out the depot.Close and setting the initial size
to 226 with identical results.
thx
md

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 16 '05 #1
2 4109
You must rewind the depot stream back to the beginning after serializing
bid1 -- otherwise, you're at the end of the stream. Try something like this
before you call Deserialize:

depot.Seek(0, SeekOrigin.Begin);

--
Mickey Williams
Author, "Microsoft Visual C# .NET Core Reference", MS Press
www.servergeek.com/blogs/mickey
"M D" <ma******@aol.com> wrote in message
news:OG***************@TK2MSFTNGP10.phx.gbl...
Please tell me why this:

static void Main(string[] args) {
Bid bid1 = new Bid(23,10.0F,3,0,DateTime.Now);
BinaryFormatter formatter = new BinaryFormatter();
Stream depot = new MemoryStream();
formatter.Serialize(depot, bid1);
Console.WriteLine("size of a bid- {0}", depot.Length);
Console.WriteLine("bid1 {0}", bid1);
Console.WriteLine(depot.CanRead);
Bid bid2 = (Bid)formatter.Deserialize(depot);
Console.WriteLine("bid2 {0}", bid2);
depot.Close();
}

results in this:

..\bin\Debug>consoleapplication1
size of a bid- 226
bid1 23:10:3:0:052405160487
True

Unhandled Exception:
System.Runtime.Serialization.SerializationExceptio n: End of
Stream encountered before parsing was completed.

Bid is:
struct Bid {
int p;
float o;
int b;
short w;
string tS;

which, in a constructor, converts the DateTime to a formatted string.

I've tried commenting out the depot.Close and setting the initial size
to 226 with identical results.
thx
md

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Nov 16 '05 #2
M D
Yes, thank you. I surmised as much and tried depot.Position = 0 before
the post even showed up.

thx
md

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 16 '05 #3

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

Similar topics

1
by: xmlguy | last post by:
PREVIOUS BACKGROUND POST: I am trying to reuse a Memory Stream for loading and transforming the xml it contains Basically I have defined following interfaces: Class Render {
3
by: Nicolas | last post by:
Hi Everybody, I'm working with the MemoryStream and I'm having problems with the ReadBytes method. When I use it, this method never returns bytes!!!! MemoryStream sw = new MemoryStream();...
2
by: SQLScott | last post by:
For some reason I am drawing a blank on this, so I would be extremely greatful if someone could help me out. I am trying to get a MemoryStream out of a Byte array. How do I get it back out? ...
13
by: Don | last post by:
When I run the following code, the MemoryStream's Position is always set to 762 instead of 0, which is what I would expect: Dim bmp As Image Dim ms As MemoryStream bmp = New...
10
by: Asaf | last post by:
Hi, I am trying to Compress & Decompress a DataSet. When running this code I am receiving the error: System.Xml.XmlException was unhandled Message="Root element is missing."...
4
by: Heron | last post by:
Hi, Could someone explain me why the following code doesn't work? The memorystream always remains with length 0. MemoryStream input = new MemoryStream();
2
by: =?Utf-8?B?RmFicmljaW8gRmVycmVpcmE=?= | last post by:
Hi, I'm facing an odd problem when consuming some web services in .Net. After call a web method, the response SOAP envelope isn't being deserialized properly. In fact, all href tags are not...
4
by: | last post by:
Hi all, I want to create a method that does the following: 1) Programmatically instantiate a new XmlDataSource control 2) For each file in a named directory, make a "FileSystemItem" element 3)...
3
by: =?Utf-8?B?UGhpbCBKb2huc29u?= | last post by:
Hi, I am using dotnet remoting with a binarry formatter. I have a property that returns a memorystream that has had a file loaded into it. When I try to access this property though I get an...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...

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.