473,799 Members | 3,147 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

A serialization problem

Hello.

I am using C# in .NET v1.1. I am trying to serialize an object to disk and
read it back again. The serialization seems to work OK (at least, it
completes without error). However, I get the following error when trying to
deserialize:

An unhandled exception of type
'System.Runtime .Serialization. SerializationEx ception' occurred in
mscorlib.dll

Additional information: Binary stream does not contain a valid BinaryHeader,
84 possible causes, invalid stream or object version change between
serialization and deserialization .

My serialization code is as follows (this.Settings is an instance of the
serializable Persistant Settings class):

public class MyApp : System.Windows. Forms.Form
{
// code stripped down...

[Serializable]
public class PersistantSetti ngs
{
// only 1 tyring to persist one object for now
public IList WatchdogToWatch ;

public PersistantSetti ngs()
{
}
}

public void SaveSettings()
{
FileStream fileStream = new
FileStream(Syst em.Windows.Form s.Application.C ommonAppDataPat h
+ SettingsFile, FileMode.OpenOr Create, FileAccess.Writ e);
BinaryFormatter formatter = new BinaryFormatter ();

this.Settings.W atchdogToWatch = toWatch; // both are declared as ILists
formatter.Seria lize(fileStream , this.Settings);
}

public bool LoadSettings()
{
FileStream fileStream;
BinaryFormatter formatter = new BinaryFormatter ();
bool fileExists = false;

fileStream = new
FileStream(Syst em.Windows.Form s.Application.C ommonAppDataPat h
+ SettingsFile, FileMode.Open, FileAccess.Read );

// this is the line that causes the error
PersistantSetti ngs settings =
(PersistantSett ings)formatter. Deserialize(fil eStream);
// etc...
}

//...
}

Here is the binary file produced by the serialization (if this is any use):

 FUwcTelcoContr ol, Version=0.0.0.0 , Culture=neutral ,
PublicKeyToken= null)Uwc.Telc oControl.Form1+ PersistantSetti ngsWatchdogTo W
atchSystem.Co llections.Array List
System.Coll ections.ArrayLi st_items_siz e_version  
 HUwcLibrary, Version=1.0.162 8.30181, Culture=neutral ,
PublicKeyToken= nullToken=null= nullUwc.Enti ties.Retailer MasterIdWholesa l
erMasterIdTyp eNameUwc. Entities.Entity Type
YUwc.En tities.EntityTy pevalue__  CH01 - Ring Phonecard

CH02 - Lime Phonecard CH 03 - Western Union
PhonecardT CH 04 - Bharath
Phonecard card  3CH03 - Western Union
PhonecardT 5CH 04 - Bharath
Phonecard8T 7$GG 01 - Global Gossip Phonecard (AUS) ?
9GG 02 - Global Community Card
< ;GG03 - BTC TravelTribe  <= GG04 - Global
Gossip TeleCard < ?GG05 - Oz Experience Phonecard
< A"GG06 - Worldwide Workers Phonecardj CGG 07 -
Nomads Phonecardk EGG 08 - Buzz Cardl GGG09 -
UWS Phonecard\ IGG 10 - StudentCard% )K GG11 -
UTS Phonecard%{ MGG 12 - Insearch
Phonecard OGG 14 - Global Gossip
TeleCode*Y QPC0 1 - Britz Phonecard+Y SPC 02 -
Maui Phonecard' UPC 03 - Alpine
Phonecard WT EST - Test product6 Y)U015 -
Backpackers World Travel Phonecard7 [!U016 - Base Backpackers
PhonecardS ]*U017 - Friendly Backpacker Group
PhonecardT _&U0 18 - Work & Travel Company Phonecard& 
aU019 - Swagmail Phonecardo zc)U0 20 - Travellers
Contact Point Phonecardo oe)U0 21 - English Language Company
Phonecard T!s gU022 - CIEE International! "
!~i#U 023 - Global Gossip Phonecard (US)")!-kU024 -
Internet Outpost#)!" mU025 - Unity Card$)!'
o'U026 - NEW Global Gossip Community
Card%Uwc.Enti ties.Wholesaler MasterIdType NameUwc.En tities.EntityTy p
e//Hq Bharath Phonecard&%s Zs
BudgetTel'% OuD EMO(%SS w
Global Gossip)%^ yGloba l Gossip Phonecard
US*%!? {Global Gossip Venice Beach+%o" }Lime
Phonecard,%S ,Pr omocom-%>? Ring
Phonecard.% ~fWe stern Union
Phonecard | .Promoco m/'>z ?Ring
Phonecard0' x?We stern Union Phonecard
Any help would be greatlyt appreciated.

Cheers,

Mike
Nov 16 '05 #1
2 1807
You problem probably is a result of the data not being completely written to
disk. Try adding the following code to the end of the SaveSettings() method

fileStream.Flus h();
fileStream.Clos e();

Also make sure you close the file stream after you load the serializaed
information.

--
Jared Parson [MSFT]
ja******@online .microsoft.com

This posting is provided "AS IS" with no warranties, and confers no rights.
Use of included script samples are subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm

"Mike" <no***@hotmail. com> wrote in message
news:ui******** *****@TK2MSFTNG P10.phx.gbl...
Hello.

I am using C# in .NET v1.1. I am trying to serialize an object to disk and read it back again. The serialization seems to work OK (at least, it
completes without error). However, I get the following error when trying to deserialize:

An unhandled exception of type
'System.Runtime .Serialization. SerializationEx ception' occurred in
mscorlib.dll

Additional information: Binary stream does not contain a valid BinaryHeader, 84 possible causes, invalid stream or object version change between
serialization and deserialization .

My serialization code is as follows (this.Settings is an instance of the
serializable Persistant Settings class):

public class MyApp : System.Windows. Forms.Form
{
// code stripped down...

[Serializable]
public class PersistantSetti ngs
{
// only 1 tyring to persist one object for now
public IList WatchdogToWatch ;

public PersistantSetti ngs()
{
}
}

public void SaveSettings()
{
FileStream fileStream = new
FileStream(Syst em.Windows.Form s.Application.C ommonAppDataPat h
+ SettingsFile, FileMode.OpenOr Create, FileAccess.Writ e);
BinaryFormatter formatter = new BinaryFormatter ();

this.Settings.W atchdogToWatch = toWatch; // both are declared as ILists
formatter.Seria lize(fileStream , this.Settings);
}

public bool LoadSettings()
{
FileStream fileStream;
BinaryFormatter formatter = new BinaryFormatter ();
bool fileExists = false;

fileStream = new
FileStream(Syst em.Windows.Form s.Application.C ommonAppDataPat h
+ SettingsFile, FileMode.Open, FileAccess.Read );

// this is the line that causes the error
PersistantSetti ngs settings =
(PersistantSett ings)formatter. Deserialize(fil eStream);
// etc...
}

//...
}

Here is the binary file produced by the serialization (if this is any use):
 FUwcTelcoContr ol, Version=0.0.0.0 , Culture=neutral ,
PublicKeyToken= null)Uwc.Telc oControl.Form1+ PersistantSetti ngsWatchdogTo W atchSystem.Co llections.Array List
System.Coll ections.ArrayLi st_items_siz e_version  
 HUwcLibrary, Version=1.0.162 8.30181, Culture=neutral ,
PublicKeyToken= nullToken=null= nullUwc.Enti ties.Retailer MasterIdWholesa l erMasterIdTyp eNameUwc. Entities.Entity Type
YUwc.En tities.EntityTy pevalue__  CH01 - Ring Phonecard

CH02 - Lime Phonecard CH 03 - Western Union
PhonecardT CH 04 - Bharath
Phonecard card 3CH03 - Western Union
PhonecardT 5CH 04 - Bharath
Phonecard8T 7$GG 01 - Global Gossip Phonecard (AUS) ?
9GG 02 - Global Community Card
< ;GG03 - BTC TravelTribe < =GG04 - Global
Gossip TeleCard < ?GG05 - Oz Experience Phonecard
< A"GG06 - Worldwide Workers Phonecardj CGG 07 - Nomads Phonecardk EGG 08 - Buzz Cardl GGG09 - UWS Phonecard\ IGG 10 - StudentCard% )K GG11 - UTS Phonecard%{ MGG 12 - Insearch
Phonecard OGG 14 - Global Gossip
TeleCode*Y QPC0 1 - Britz Phonecard+Y SPC 02 -
Maui Phonecard' UPC 03 - Alpine
Phonecard WT EST - Test product6 Y)U015 -
Backpackers World Travel Phonecard7 [!U016 - Base Backpackers PhonecardS ]*U017 - Friendly Backpacker Group
PhonecardT _&U0 18 - Work & Travel Company Phonecard& 
aU019 - Swagmail Phonecardo zc)U0 20 - Travellers
Contact Point Phonecardo oe)U0 21 - English Language Company
Phonecard T!s gU022 - CIEE International! "
!~i#U 023 - Global Gossip Phonecard (US)")!-kU024 -
Internet Outpost#)!" mU025 - Unity Card$)!'
o'U026 - NEW Global Gossip Community
Card%Uwc.Enti ties.Wholesaler MasterIdType NameUwc.En tities.EntityTy p e//Hq Bharath Phonecard&%s Zs
BudgetTel'% OuD EMO(%SS w
Global Gossip)%^ yGloba l Gossip Phonecard
US*%!? {Global Gossip Venice Beach+%o" }Lime
Phonecard,%S ,Pr omocom-%>? Ring
Phonecard.% ~fWe stern Union
Phonecard |.Pr omocom/'>z ?Ring
Phonecard0' x?We stern Union Phonecard
Any help would be greatlyt appreciated.

Cheers,

Mike

Nov 16 '05 #2
> You problem probably is a result of the data not being completely written
to
disk. Try adding the following code to the end of the SaveSettings() method
fileStream.Flus h();
fileStream.Clos e();


That did it, thanks.

Mike
Nov 16 '05 #3

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

Similar topics

4
2114
by: David K | last post by:
we are having a problem when trying to serilalize and deserialize DataTable When replacing the container to be ListArray or HashTable everything works fine The code is very straight forward ( see below), however when comparing the DataTable before and after the serialize, it is not the same The DataTable after the serialization contains only the string of class type ( Vclass in this class) but not the class itself Any idea ?
37
5024
by: Ben | last post by:
Hi, there. Recently I was working on a problem where we want to save generic closures in a data structure (a vector). The closure should work for any data type and any method with pre-defined signature. When developing this lib, I figured that the pointer-to-member-function, although seemingly an attractive solution, does not work well for us.
0
1237
by: ktn | last post by:
Hi all, I'm a .NET beginner and I've got a problem on a program where I try to do an XML serialization. I get the following error : "An unmanaged exception of type 'System.IO.FileNotFoundException' occured in mscorlib.dll Additonal information : the file or asssembly named n9gu4-bo.dll or one of its dependencies cannot be found"
1
4365
by: andrewcw | last post by:
There is an error in XML document (1, 2). I used XML spy to create the XML and XSD. When I asked to have the XML validated it said it was OK. I used the .net SDK to generate the class. I have done this before but this time I have no idea why I am getting the error. Any ideas ?? THANKS ! <qcsttatus> <manual sourcerootfolder="" manualname="">
2
2358
by: ofer | last post by:
Hi, I am working with the beta version of the new .net framework (Whidbey) and I encountered a problem with serialization that did'nt exist in the .net 2003 the situation is like this : I have a class that inherits from dataset, and I want to serialize it , so I created a serialization constructor that forwards the call to the base class (the dataset) serialization constructor, normally, for this action to succeed I am supposed to put...
0
2507
by: umhlali | last post by:
I get the following exception when my VB.NET app calls a Java web service that returns an array of objects. The same call works for a single object though. So looks like there is no problem serializing the object but there seems to be a problem serializing an array of objects. Any help will be appreciated "Cannot assign object of type System.Object to an object of type ElectronicWallet.C2PTest.PaymentItem." :...
5
6030
by: Harold Howe | last post by:
I am having a problem deserializing objects from a library when the following conditions exist: 1- The library is strongly named 2- The serialized file was created with version 1.0 of the assembly 3- I am trying to deserialize from an EXE that references version 2.0 of the assembly 4- Both version 1.0 and 2.0 of the assembly reside in the GAC (no policy redirects exist).
2
1633
by: Norman Chong | last post by:
Hiddeldi ho, I want to save an object so that I can use its content after I restart my program. I tried to solve this with serialization because someone told me that this is the correct way for this. So I wrote the following code to serialize\deserialize the object, but now I have the problem that the object has a generic list and when I'm trying to deserialize it, I get an error. <System.Serializable()Public Class TestClass
4
11417
by: mijalko | last post by:
Hi, I have inherited my class from System.Drawing.Printing.PrintDocument and I wish to serialize this object using XmlSerializer. And I get exception "There was an error reflecting type ...". If I look at innerException it says: "Cannot serialize member 'System.ComponentModel.Component.Site' of type 'System.ComponentModel.ISite'. OK it is problem to serialize all data so I'll implement my Serialization. I implemented ISerializable...
2
5568
by: mkvenkit.vc | last post by:
Hello, I hope this is the right place to post a question on Boost. If not, please let me know where I can post this message and I will do so. I am having a strange problem with std::string as I am trying to read from a binary archive using Boost serialization. I am new to this, and it is possible that I have not understood the usage. In the code below, the string "faultblock" seems to be causing the problem. The code crashes in the ia...
0
10470
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10247
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10214
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,...
1
7561
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
6803
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
5459
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5583
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4135
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
2
3751
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.