Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 2005 > C# 2005
| Search | Today's Posts | Mark Forums Read
C# 2005 For discussion of Visual C# 2005.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# 2005 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
Old July 16th, 2007, 12:17 PM
Registered User
Join Date: Mar 2007
Location: , , .
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default Deserialization of a file

I have a file with an object serialized with System.Runtime.Serialization.Formatters.Binary.Bin aryFormatter. To ensure that the file is written properly, I append a 64 character checksum to the end. I can pull out and verify the checksum, but I can't figure out how to get the BinaryFormatter to deserialize the data. I can't have it deserialize the file stream, because it has the checksum at the end.

If I:
  • Open the file
  • Read it with a StreamReader
  • Output the stream to a string
  • Remove the checksum
  • Convert the string to a byte[] using System.Text.Encoding.Ascii.GetBytes()
  • Put the bytes into a MemoryStream
  • Deserialize the MemoryStream
I get this error:
Binary stream '0' does not contain a valid BinaryHeader. Possible causes are invalid stream or object version change between serialization and deserialization.
Old July 16th, 2007, 02:08 PM
planoie's Avatar
Friend of Wrox
Points: 16,481, Level: 55
Points: 16,481, Level: 55 Points: 16,481, Level: 55 Points: 16,481, Level: 55
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
Join Date: Aug 2003
Location: Clifton Park, New York, USA.
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts

What about manually reading in the stream, removing the last 64 bytes then deserialize?

Old July 16th, 2007, 08:11 PM
Registered User
Join Date: Mar 2007
Location: , , .
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts

But that's what I'm doing. Here is my entire method:

private void loadChecksummedFile(object obj, string fileExension)
    FileStream fs = new FileStream(m_filePath + "." + fileExension, FileMode.Open, FileAccess.ReadWrite);
    BinaryFormatter bf = new BinaryFormatter();
    StreamReader sr = new StreamReader(fs);
    MemoryStream ms;
    MD5 checksum = MD5.Create();
    string fileString;
    string checksumInFile;
    byte[] data;
    string newChecksum;

    fileString = sr.ReadToEnd(); // Read the file into a string
    checksumInFile = fileString.Substring(fileString.Length - 64, 64); // Pull out the checksum
    data = System.Text.Encoding.ASCII.GetBytes(fileString.Substring(0, fileString.Length - 64)); //Take the string (minus the 
                                                                                                //checksum) and put it in a byte[]
    // Compute the checksum and compare
    newChecksum = checksumToString(checksum.ComputeHash(data)); 
    if(checksumInFile == newChecksum)
        ms = new MemoryStream(data); //Put the byte[] of data into a memory stream because BinaryFormatter can only Deserialize 
        obj = (object[]) bf.Deserialize(ms); //Error occurs here
        //TODO: Load the backup


Similar Threads
Thread Thread Starter Forum Replies Last Post
Serialization and DeSerialization balesh.mind ASP.NET 2.0 Professional 0 September 12th, 2007 05:05 AM
Serialization & Deserialization kadimijagan Pro Java 2 September 7th, 2007 10:13 AM
Read Text file and convert to Binary file VB.net sjlsysprg1 Pro VB.NET 2002/2003 4 June 29th, 2007 06:53 AM
How to display .tif file, .pdf file and .jpg file phuong171 ASP.NET 1.0 and 1.1 Basics 1 March 13th, 2007 07:29 AM

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.