[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 223
  • Last Modified:

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.. ??
0
simlox
Asked:
simlox
  • 4
1 Solution
 
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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
 
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

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now