Re: A serialization problem

Tech-Archive recommends: Fix windows errors by optimizing your registry

From: Jared Parsons [MSFT] (jaredpar_at_online.microsoft.com)
Date: 06/16/04


Date: Wed, 16 Jun 2004 10:49:33 -0700

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]
jaredpar@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" <noone@hotmail.com> wrote in message
news:uilCwN3UEHA.412@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.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
> 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.WholesalerMasterIdTypeNameUwc.Entities.EntityTyp
> e//HqBharath Phonecard&%sZs
> BudgetTel'%OuDEMO(%SSw
> 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
>
>


Relevant Pages

  • A serialization problem
    ... The serialization seems to work OK (at least, ... public class PersistantSettings ... BinaryFormatter formatter = new BinaryFormatter; ... Gossip TeleCard <?GG05 - Oz Experience Phonecard ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Newbie Q: Serialization and Me
    ... Dim bf As BinaryFormatter = New BinaryFormatter ... I have 2 objects and I want to make a deep copy of one into the ... Target as T) as Integer 'use serialization to make a deep ...
    (microsoft.public.dotnet.languages.vb)
  • RE: Challenge? [De-]Serialise a class derived from DataTable.
    ... Have you tried binary serialization? ... structures) when i deserialize them i get them back in the original ... Dim m As New MemoryStream ... Dim b As New BinaryFormatter() ...
    (microsoft.public.dotnet.languages.vb)
  • Question about data size when using serialized writing using BinaryFormatter
    ... I am writing a structure marked as serializable to a data file using the ... The structure does not inherit from any other types. ... BinaryFormatter, the resulting file size is much more. ... with the storage during serialization? ...
    (microsoft.public.dotnet.framework.clr)