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.. ??
simloxAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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

Cheers

Wint.
0
WinterMuteUKCommented:
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.
0
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

WinterMuteUKCommented:
This is the version for the collections, (copying and pasting this code should work straight off!):

//CODE FOLLOWS
using System;
using System.Collections;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;

class Runner
{
      /// <summary>
      /// The main entry point for the application.
      /// </summary>
      static void Main(string[] args)
      {
            Console.Write("Serialise....");
            TestSerializer ts = new TestSerializer();
            ts.SerialiseCollection();
            Console.WriteLine("...Serialised. Press ENTER to continue");
            Console.ReadLine();
            Console.WriteLine("Deserialising");
            ArrayList arr = ts.DeserialiseCollection();
            foreach(MyTestClass m in arr)
                  Console.WriteLine(m.ToString());
            Console.WriteLine("Deserialised. Press ENTER to continue");
            Console.ReadLine();
      }
}

[Serializable]
public class MyTestClass
{
      private int i;
      private double db;
      public MyTestClass(int i, double db)
      {
            this.i = i;
            this.db = db;
      }
      public override string ToString()
      {
            return "i = " + i + "; db = " + db + "\r\n";
      }
}

public class TestSerializer
{
      public void SerialiseCollection()
      {
            ArrayList arrList = new ArrayList();

            MyTestClass myTest = new MyTestClass(1, 2.3);
            arrList.Add(myTest);

            myTest = new MyTestClass(3, 44.3);
            arrList.Add(myTest);

            FileInfo f = new FileInfo(@"C:\outputArr.dat");
            BinaryFormatter b = new BinaryFormatter();

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

      public ArrayList DeserialiseCollection()
      {
            FileInfo f = new FileInfo(@"c:\outputArr.dat");
            BinaryFormatter bf = new BinaryFormatter();
            Stream sw = f.Open(FileMode.Open, FileAccess.Read);
              
            ArrayList arrList = bf.Deserialize(sw) as ArrayList;
            return arrList;
      }
}

Cheers
Wint.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
simloxAuthor Commented:
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
0
WinterMuteUKCommented:
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.

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.