By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,008 Members | 2,963 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,008 IT Pros & Developers. It's quick & easy.

A serialization problem

P: n/a
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.SerializationExcepti on' 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 PersistantSettings
{
// only 1 tyring to persist one object for now
public IList WatchdogToWatch;

public PersistantSettings()
{
}
}

public void SaveSettings()
{
FileStream fileStream = new
FileStream(System.Windows.Forms.Application.Common AppDataPath
+ SettingsFile, FileMode.OpenOrCreate, FileAccess.Write);
BinaryFormatter formatter = new BinaryFormatter();

this.Settings.WatchdogToWatch = toWatch; // both are declared as ILists
formatter.Serialize(fileStream, this.Settings);
}

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

fileStream = new
FileStream(System.Windows.Forms.Application.Common AppDataPath
+ SettingsFile, FileMode.Open, FileAccess.Read);

// this is the line that causes the error
PersistantSettings settings =
(PersistantSettings)formatter.Deserialize(fileStre am);
// etc...
}

//...
}

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

 FUwcTelcoControl, Version=0.0.0.0, Culture=neutral,
PublicKeyToken=null)Uwc.TelcoControl.Form1+Persi stantSettingsWatchdogToW
atchSystem.Collections.ArrayList
System.Collections.ArrayList_items_size_ve rsion  
 HUwcLibrary, Version=1.0.1628.30181, Culture=neutral,
PublicKeyToken=nullToken=null=nullUwc.Entities. RetailerMasterIdWholesal
erMasterIdTypeNameUwc.Entities.EntityType
YUwc.Entities.EntityTypevalue__  CH01 - Ring Phonecard

CH02 - Lime PhonecardCH03 - Western Union
PhonecardTCH04 - Bharath
Phonecard card3CH03 - Western Union
PhonecardT5CH04 - Bharath
Phonecard8T7$GG01 - Global Gossip Phonecard (AUS) ?
9GG02 - Global Community Card
<;GG03 - BTC TravelTribe <=GG04 - Global
Gossip TeleCard <?GG05 - Oz Experience Phonecard
<A"GG06 - Worldwide Workers PhonecardjCGG07 -
Nomads PhonecardkEGG08 - Buzz CardlGGG09 -
UWS Phonecard\IGG10 - StudentCard%)KGG11 -
UTS Phonecard%{MGG12 - Insearch
PhonecardOGG14 - Global Gossip
TeleCode*YQPC01 - Britz Phonecard+YSPC02 -
Maui Phonecard'UPC03 - Alpine
PhonecardWTEST - Test product6Y)U015 -
Backpackers World Travel Phonecard7[!U016 - Base Backpackers
PhonecardS]*U017 - Friendly Backpacker Group
PhonecardT_&U018 - Work & Travel Company Phonecard& 
aU019 - Swagmail Phonecardo zc)U020 - Travellers
Contact Point Phonecardo oe)U021 - English Language Company
Phonecard T!sgU022 - CIEE International!"
!~i#U023 - Global Gossip Phonecard (US)")!-kU024 -
Internet Outpost#)!"mU025 - Unity Card$)!'
o'U026 - NEW Global Gossip Community
Card%Uwc.Entities.WholesalerMasterIdTypeName Uwc.Entities.EntityTyp
e//HqBharath Phonecard&%sZs
BudgetTel'%OuDEMO(%SS w
Global Gossip)%^yGlobal Gossip Phonecard
US*%!?{Global Gossip Venice Beach+%o"}Lime
Phonecard,%S,Promocom-%>?Ring
Phonecard.%~fWestern Union
Phonecard |.Promocom/'>z?Ring
Phonecard0'x?Western Union Phonecard
Any help would be greatlyt appreciated.

Cheers,

Mike
Nov 16 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
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.Flush();
fileStream.Close();

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*************@TK2MSFTNGP10.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.SerializationExcepti on' 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 PersistantSettings
{
// only 1 tyring to persist one object for now
public IList WatchdogToWatch;

public PersistantSettings()
{
}
}

public void SaveSettings()
{
FileStream fileStream = new
FileStream(System.Windows.Forms.Application.Common AppDataPath
+ SettingsFile, FileMode.OpenOrCreate, FileAccess.Write);
BinaryFormatter formatter = new BinaryFormatter();

this.Settings.WatchdogToWatch = toWatch; // both are declared as ILists
formatter.Serialize(fileStream, this.Settings);
}

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

fileStream = new
FileStream(System.Windows.Forms.Application.Common AppDataPath
+ SettingsFile, FileMode.Open, FileAccess.Read);

// this is the line that causes the error
PersistantSettings settings =
(PersistantSettings)formatter.Deserialize(fileStre am);
// etc...
}

//...
}

Here is the binary file produced by the serialization (if this is any use):
 FUwcTelcoControl, Version=0.0.0.0, Culture=neutral,
PublicKeyToken=null)Uwc.TelcoControl.Form1+Persi stantSettingsWatchdogToW atchSystem.Collections.ArrayList
System.Collections.ArrayList_items_size_ve rsion  
 HUwcLibrary, Version=1.0.1628.30181, Culture=neutral,
PublicKeyToken=nullToken=null=nullUwc.Entities. RetailerMasterIdWholesal erMasterIdTypeNameUwc.Entities.EntityType
YUwc.Entities.EntityTypevalue__  CH01 - Ring Phonecard

CH02 - Lime PhonecardCH03 - Western Union
PhonecardTCH04 - Bharath
Phonecard card3CH03 - Western Union
PhonecardT5CH04 - Bharath
Phonecard8T7$GG01 - Global Gossip Phonecard (AUS) ?
9GG02 - Global Community Card
<;GG03 - BTC TravelTribe <=GG04 - Global
Gossip TeleCard <?GG05 - Oz Experience Phonecard
<A"GG06 - Worldwide Workers PhonecardjCGG07 - Nomads PhonecardkEGG08 - Buzz CardlGGG09 - UWS Phonecard\IGG10 - StudentCard%)KGG11 - UTS Phonecard%{MGG12 - Insearch
PhonecardOGG14 - Global Gossip
TeleCode*YQPC01 - Britz Phonecard+YSPC02 -
Maui Phonecard'UPC03 - Alpine
PhonecardWTEST - Test product6Y)U015 -
Backpackers World Travel Phonecard7[!U016 - Base Backpackers PhonecardS]*U017 - Friendly Backpacker Group
PhonecardT_&U018 - Work & Travel Company Phonecard& 
aU019 - Swagmail Phonecardo zc)U020 - Travellers
Contact Point Phonecardo oe)U021 - English Language Company
Phonecard T!sgU022 - CIEE International!"
!~i#U023 - Global Gossip Phonecard (US)")!-kU024 -
Internet Outpost#)!"mU025 - Unity Card$)!'
o'U026 - NEW Global Gossip Community
Card%Uwc.Entities.WholesalerMasterIdTypeName Uwc.Entities.EntityTyp e//HqBharath Phonecard&%sZs
BudgetTel'%OuDEMO(%SS w
Global Gossip)%^yGlobal Gossip Phonecard
US*%!?{Global Gossip Venice Beach+%o"}Lime
Phonecard,%S,Promocom-%>?Ring
Phonecard.%~fWestern Union
Phonecard |.Promocom/'>z?Ring
Phonecard0'x?Western Union Phonecard
Any help would be greatlyt appreciated.

Cheers,

Mike

Nov 16 '05 #2

P: n/a
> 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.Flush();
fileStream.Close();


That did it, thanks.

Mike
Nov 16 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.