Link to home
Start Free TrialLog in
Avatar of simlox
simloxFlag for United Kingdom of Great Britain and Northern Ireland

asked on

serialise a c# object based on a collection

ive got an object x1 with 2 vars v1 and v2 both protected (also tried them public)..
x1Col contains an arrayList of x1 objects
x1ColCats contains an arrayList of x1Cols objects..

I want to serialise the x1ColCats object to an xml file then reload back from the file once the app is restarted.  

ive tried using a xmlserialiser object and using a streamwriter to write it out.. this works for the single x1 object but i cannot get it to work for the others..

Any ideas guys.. or have i missed something important out in the process.. ??
Avatar of rfgkev
rfgkev

did u put the [Serializable()]  attribute tag on your object?
From memory the XmlSerialiser doesn't work on collections, you'd need to use the Binary or Soap Serialisers instead..

Cheers

Wint.
This is a way of using the BinaryFormatter to format the collections to a file:

//CODE FOLLOWS

[Serializable]
public class MyTestClass
{
      private int i;
      private double db;
      public MyTestClass(int i, double db)
      {
            this.i = i;
            this.db = db;
      }
}

public class TestSerializer
{
      public void Serialise()
      {
            MyTestClass myTest = new MyTestClass(1, 2.3);
            FileInfo f = new FileInfo(@"C:\output.dat");
            BinaryFormatter b = new BinaryFormatter();

            Stream sw = f.Open(FileMode.Append, FileAccess.Write);
            b.Serialize(sw, myTest);
            sw.Close();
      }

      public MyTestClass Deserialise()
      {
            FileInfo f = new FileInfo(@"c:\output.dat");
            BinaryFormatter bf = new BinaryFormatter();
            Stream sw = f.Open(FileMode.Open, FileAccess.Read);
       
            MyTestClass myTest = bf.Deserialize(sw) as MyTestClass;
            return myTest;
      }
}

Cheers

Wint.
ASKER CERTIFIED SOLUTION
Avatar of WinterMuteUK
WinterMuteUK
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of simlox

ASKER

it wasnt quite what i wanted but it'll do the trick... thx a lot for a fast response..

an additional piece of info that may be handy.. I had a extended hash table as the parent object with extended arraylist object as a child and a number of classes in the arraylist. The hashtable would'nt deserialise properly though until i added a custom protected constructor to the parent object (the one inheriting from hashtable) as follows:

protected catCol(SerializationInfo info, StreamingContext context):base(info,context)
{}

this solved the 'the constructor to deserialize an object of type cols.catCol was not found' error message..

Once again.. thx
Your welcome,

The constructor problem is a problem that also occurs when extending exceptions (I found this out when attempting to throw exceptions over a remoting boundary).

Cheers

Wint.