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

Binary stream does not contain a valid BinaryHeader

BH
I'm trying a simple object serialization and deserialization, and keep
getting this error:

System.Runtime.Serialization.SerializationExceptio n: Binary stream does not
contain a valid BinaryHeader, 0 possible causes, invalid stream or object
version change between serialization and deserialization.

Here's my code. it does nothing but to serialize a DataTable object into a
byte array, and then read the byte array back for deserialization. I
verified that the byte array length did not change. the error occurs on the
object newObj = formatter.Deserialize(ms2); line.

public class Test {
Public Test {

DataTable dt = this.CreateDataSource();
System.IO.Stream ms= new System.IO.MemoryStream();
IFormatter formatter = new BinaryFormatter();
formatter.Serialize(ms, dt);

int numBytesToRead = (int) ms.Length;
byte[] bytes = new byte[numBytesToRead];
int numBytesRead = 0;
int n = ms.Read(bytes, numBytesRead, numBytesToRead);
ms.Position = 0;
ms.Close();

System.IO.Stream ms2= new System.IO.MemoryStream();
int numBytesToWrite = bytes.Length;
ms2.SetLength(numBytesToWrite);
int numBytesWritten = 0;
ms2.Write(bytes, numBytesWritten, numBytesToWrite);

ms2.Position = 0;
object newObj = formatter.Deserialize(ms2);

ms2.Close();
}
private DataTable CreateDataSource() {
DataTable dt = new DataTable();
DataRow dr;

dt.Columns.Add(new DataColumn("IntegerValue", typeof(Int32)));
dt.Columns.Add(new DataColumn("StringValue", typeof(string)));
dt.Columns.Add(new DataColumn("CurrencyValue", typeof(double)));

for (int i = 0; i < 10; i++) {
dr = dt.NewRow();

dr[0] = i;
dr[1] = "Item " + i.ToString();
dr[2] = 1.23 * (i+1);

dt.Rows.Add(dr);
}
return dt;
}
}

Nov 17 '05 #1
1 4250
BH <bo*******@yahoo.com> wrote:
I'm trying a simple object serialization and deserialization, and keep
getting this error:

System.Runtime.Serialization.SerializationExceptio n: Binary stream does not
contain a valid BinaryHeader, 0 possible causes, invalid stream or object
version change between serialization and deserialization.

Here's my code.
That's not your code. When you're going to post code, *please* post the
actual code - the code you posted doesn't compile. However, when
(adding using statements, etc) I got your code to compile, the problem
is pretty simple - and it's nothing to do with serialization, really.
public class Test {
Public Test {

DataTable dt = this.CreateDataSource();
System.IO.Stream ms= new System.IO.MemoryStream();
IFormatter formatter = new BinaryFormatter();
formatter.Serialize(ms, dt);

int numBytesToRead = (int) ms.Length;
byte[] bytes = new byte[numBytesToRead];
int numBytesRead = 0;
int n = ms.Read(bytes, numBytesRead, numBytesToRead);
ms.Position = 0;
ms.Close();


All the mistakes are in this section.

Firstly, you're trying to read *before* rewinding the stream - in other
words, you write to the stream, but then try to read *from the end*.

Secondly, when you read you're trying to read, you're trying to read at
the end of the byte array rather than the start.

Thirdly, you're assuming that everything will be read in one chunk
(which it probably will with a MemoryStream, but don't make that
assumption for streams in general). See
http://www.yoda.arachsys.com/csharp/readbinary.html for more details.

Fourthly, you're not flushing the stream after writing to it. Close()
will do this for you, and in a MemoryStream it's probably not necessary
anyway, but if you're going to do anything other than write then close,
I'd recommend calling flush anyway.

Now, taking the above points into consideration (except number 3 for
the moment, because it wasn't harming your code in reality, would be a
minor pain to demonstrate, is unnecessary for reasons explained in a
moment, and probably wouldn't be what you'd use in real serialization)
we end up with:

DataTable dt = CreateDataSource();
System.IO.Stream ms= new System.IO.MemoryStream();
IFormatter formatter = new BinaryFormatter();
formatter.Serialize(ms, dt);
ms.Flush();
ms.Position = 0;

int numBytesToRead = (int) ms.Length;
byte[] bytes = new byte[numBytesToRead];
ms.Read(bytes, 0, numBytesToRead);
ms.Close();

which works.

However, simpler is:
DataTable dt = CreateDataSource();
System.IO.Stream ms= new System.IO.MemoryStream();
IFormatter formatter = new BinaryFormatter();
formatter.Serialize(ms, dt);
ms.Flush();
byte[] bytes = ms.ToArray();
ms.Close();

Of course, simpler than that is just to rewind the memory stream and
use the same stream for deserializing as we just serialized into:

DataTable dt = CreateDataSource();
System.IO.Stream ms= new System.IO.MemoryStream();
IFormatter formatter = new BinaryFormatter();
formatter.Serialize(ms, dt);
ms.Flush();
ms.Position=0;
object newObj = formatter.Deserialize(ms);
ms.Close();

but maybe that doesn't show what you wanted to show.

Anyway, hope this helped.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet/
If replying to the group, please do not mail me too
Nov 17 '05 #2

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

Similar topics

4
by: nightflyer | last post by:
Hi all, [code snippet appended at the end.) my question: A class has a few string variables with not know length at design time. Now I declare lets say a 1000 of those classes and put them...
103
by: Steven T. Hatton | last post by:
§27.4.2.1.4 Type ios_base::openmode Says this about the std::ios::binary openmode flag: *binary*: perform input and output in binary mode (as opposed to text mode) And that is basically _all_ it...
1
by: BH | last post by:
I'm trying a simple object serialization and deserialization, and keep getting this error: System.Runtime.Serialization.SerializationException: Binary stream does not contain a valid...
5
by: Neo | last post by:
Hello: I am receiving a Binary File in a Request from a application. The stream which comes to me has the boundary (Something like "---------------------------39<WBR>­0C0F3E0099" without the...
3
by: Arun | last post by:
Hi, I have simple question to ask. How to write multiple Binary files to the Browser using Asp.Net and Visual C#.net I have seen examples where single binary file is written to browser. ...
12
by: Adam J. Schaff | last post by:
I am writing a quick program to edit a binary file that contains file paths (amongst other things). If I look at the files in notepad, they look like: ...
68
by: vim | last post by:
hello everybody Plz tell the differance between binary file and ascii file............... Thanks in advance vim
12
by: Registered User | last post by:
I've read in a book: <quote> With a binary-mode stream, you can't detect the end-of-file by looking for EOF, because a byte of data from a binary stream could have that value, which would...
0
by: Vince Filby | last post by:
Hi, We are working with distributing Lucene.net. We have Master Index Server which takes responsibility of distributing the index searching to multiple Index Servers by calling the remote...
1
by: sandeepbhutani304 | last post by:
have 2 projects communicating each other with .NET remoting. But when I am trying to call these functions I am getting the error: The input stream is not a valid binary format. The starting...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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...
0
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
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...

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.