A serialization problem

M

Mike

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.SerializationException' 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.CommonAppDataPath
+ 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.CommonAppDataPath
+ SettingsFile, FileMode.Open, FileAccess.Read);

// this is the line that causes the error
PersistantSettings settings =
(PersistantSettings)formatter.Deserialize(fileStream);
// 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+PersistantSettingsWatchdogToW
atchSystem.Collections.ArrayList
System.Collections.ArrayList_items_size_version  
 HUwcLibrary, Version=1.0.1628.30181, Culture=neutral,
PublicKeyToken=nullToken=null=nullUwc.Entities.RetailerMasterIdWholesal
erMasterIdTypeNameUwc.Entities.EntityType
YöÿÿÿUwc.Entities.EntityTypevalue__  CH01 - Ring Phonecard
ôÿÿÿöÿÿÿ
CH02 - Lime Phonecard¡òÿÿÿöÿÿÿCH03 - Western Union
PhonecardTÅðÿÿÿöÿÿÿCH04 - Bharath
Phonecard card¡ÎÿÿÿÐÿÿÿ3CH03 - Western Union
PhonecardTÅÌÿÿÿÐÿÿÿ5CH04 - Bharath
Phonecard8TÊÿÿÿÐÿÿÿ7$GG01 - Global Gossip Phonecard (AUS) ?Ö
ÈÿÿÿÐÿÿÿ9GG02 - Global Community Card
ô<ÆÿÿÿÐÿÿÿ;GG03 - BTC TravelTribe õ<ÄÿÿÿÐÿÿÿ=GG04 - Global
Gossip TeleCard ö<ÂÿÿÿÐÿÿÿ?GG05 - Oz Experience Phonecard
÷<ÀÿÿÿÐÿÿÿA"GG06 - Worldwide Workers Phonecardj̾ÿÿÿÐÿÿÿCGG07 -
Nomads Phonecardk̼ÿÿÿÐÿÿÿEGG08 - Buzz Cardl̺ÿÿÿÐÿÿÿGGG09 -
UWS Phonecard\¸ÿÿÿÐÿÿÿIGG10 - StudentCard%)¶ÿÿÿÐÿÿÿKGG11 -
UTS Phonecard%{´ÿÿÿÐÿÿÿMGG12 - Insearch
PhonecardÈç²ÿÿÿÐÿÿÿOGG14 - Global Gossip
TeleCode*Y°ÿÿÿÐÿÿÿQPC01 - Britz Phonecard+Y®ÿÿÿÐÿÿÿSPC02 -
Maui Phonecard'¬ÿÿÿÐÿÿÿUPC03 - Alpine
PhonecardöÿÿÿªÿÿÿÐÿÿÿWTEST - Test product6ô¨ÿÿÿÐÿÿÿY)U015 -
Backpackers World Travel Phonecard7ô¦ÿÿÿÐÿÿÿ[!U016 - Base Backpackers
PhonecardSä¤ÿÿÿÐÿÿÿ]*U017 - Friendly Backpacker Group
PhonecardTä¢ÿÿÿÐÿÿÿ_&U018 - Work & Travel Company Phonecard½& 
ÿÿÿÐÿÿÿaU019 - Swagmail Phonecardo zÿÿÿÐÿÿÿc)U020 - Travellers
Contact Point Phonecardo oÿÿÿÐÿÿÿe)U021 - English Language Company
Phonecard T!sÿÿÿÐÿÿÿgU022 - CIEE International!"
!~ÿÿÿÐÿÿÿi#U023 - Global Gossip Phonecard (US)"¥)!-ÿÿÿÐÿÿÿkU024 -
Internet Outpost#¦)!"ÿÿÿÐÿÿÿmU025 - Unity Card$§)!'
ÿÿÐÿÿÿo'U026 - NEW Global Gossip Community
Card%Uwc.Entities.WholesalerMasterIdTypeNameUwc.Entities.EntityTyp
e//HÿÿÿÐÿÿÿqBharath Phonecard&%s´ZÿÿÿÐÿÿÿs
BudgetTel'%þÿÿÿOÿÿÿÐÿÿÿuDEMO(%Ç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
 
J

Jared Parsons [MSFT]

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]
(e-mail address removed)

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 said:
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.SerializationException' 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.CommonAppDataPath
+ 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.CommonAppDataPath
+ SettingsFile, FileMode.Open, FileAccess.Read);

// this is the line that causes the error
PersistantSettings settings =
(PersistantSettings)formatter.Deserialize(fileStream);
// 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+PersistantSettingsWatchdogToW
atchSystem.Collections.ArrayList
System.Collections.ArrayList_items_size_version  
 HUwcLibrary, Version=1.0.1628.30181, Culture=neutral,
PublicKeyToken=nullToken=null=nullUwc.Entities.RetailerMasterIdWholesal
erMasterIdTypeNameUwc.Entities.EntityType
YöÿÿÿUwc.Entities.EntityTypevalue__  CH01 - Ring Phonecard
ôÿÿÿöÿÿÿ
CH02 - Lime Phonecard¡òÿÿÿöÿÿÿCH03 - Western Union
PhonecardTÅðÿÿÿöÿÿÿCH04 - Bharath
Phonecard card¡ÎÿÿÿÐÿÿÿ3CH03 - Western Union
PhonecardTÅÌÿÿÿÐÿÿÿ5CH04 - Bharath
Phonecard8TÊÿÿÿÐÿÿÿ7$GG01 - Global Gossip Phonecard (AUS) ?Ö
ÈÿÿÿÐÿÿÿ9GG02 - Global Community Card
ô<ÆÿÿÿÐÿÿÿ;GG03 - BTC TravelTribe õ<ÄÿÿÿÐÿÿÿ=GG04 - Global
Gossip TeleCard ö<ÂÿÿÿÐÿÿÿ?GG05 - Oz Experience Phonecard
÷<ÀÿÿÿÐÿÿÿA"GG06 - Worldwide Workers
Phonecardj̾ÿÿÿÐÿÿÿCGG07 -
Nomads Phonecardk̼ÿÿÿÐÿÿÿEGG08 - Buzz
Cardl̺ÿÿÿÐÿÿÿGGG09 -
UWS Phonecard\¸ÿÿÿÐÿÿÿIGG10 -
StudentCard%)¶ÿÿÿÐÿÿÿKGG11 -
UTS Phonecard%{´ÿÿÿÐÿÿÿMGG12 - Insearch
PhonecardÈç²ÿÿÿÐÿÿÿOGG14 - Global Gossip
TeleCode*Y°ÿÿÿÐÿÿÿQPC01 - Britz Phonecard+Y®ÿÿÿÐÿÿÿSPC02 -
Maui Phonecard'¬ÿÿÿÐÿÿÿUPC03 - Alpine
PhonecardöÿÿÿªÿÿÿÐÿÿÿWTEST - Test product6ô¨ÿÿÿÐÿÿÿY)U015 -
Backpackers World Travel Phonecard7ô¦ÿÿÿÐÿÿÿ[!U016 - Base Backpackers
PhonecardSä¤ÿÿÿÐÿÿÿ]*U017 - Friendly Backpacker Group
PhonecardTä¢ÿÿÿÐÿÿÿ_&U018 - Work & Travel Company Phonecard½& 
ÿÿÿÐÿÿÿaU019 - Swagmail Phonecardo zÿÿÿÐÿÿÿc)U020 - Travellers
Contact Point Phonecardo oÿÿÿÐÿÿÿe)U021 - English Language Company
Phonecard T!sÿÿÿÐÿÿÿgU022 - CIEE International!"
!~ÿÿÿÐÿÿÿi#U023 - Global Gossip Phonecard (US)"¥)!-ÿÿÿÐÿÿÿkU024 -
Internet Outpost#¦)!"ÿÿÿÐÿÿÿmU025 - Unity Card$§)!'
ÿÿÐÿÿÿo'U026 - NEW Global Gossip Community
Card%Uwc.Entities.WholesalerMasterIdTypeNameUwc.Entities.EntityTyp
e//HÿÿÿÐÿÿÿqBharath Phonecard&%s´ZÿÿÿÐÿÿÿs
BudgetTel'%þÿÿÿOÿÿÿÐÿÿÿuDEMO(%Ç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
 
M

Mike

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
 
Top