none
How can Serialize ArrayList ?

    Question

  • Hi

    code sample?????????


    public class Employee :ISerializable
    {
    private int version =1;

    //ADD emoployee name in this list
    private ArrayList employeeList;

    private void  AddEmployee()
    {
     // Adding employee to Employee List
    }
    protected TestData(SerializationInfo info,StreamingContext context)
    {
    this.version = info.GetInt32("version");
    this.name = info.GetString("list");
    }

    void ISerializable.GetObjectData(SerializationInfo info, StreamingContext context)
    {
    info.AddValue("version", this.version);
    info.AddValue("list", this.employeeList);
    }

    }

     

    Thanks in

    Advance

    nayakam

    Monday, September 25, 2006 12:29 PM

Answers

  • indeed, there are no special ways. What errors are you getting if any? you may need to perhaps add an extra type also into the serializer class so it knows what to expect when serializing/deserializing.

    Example I had a class, it contained an ArrayList. This is my class and this is how I would serialize/deserialize it:


    public class ClassA

    {
    private ArrayList Events;
     
    public ClassA()
    {
       this.Events =
    new ArrayList();
       this.Events.Add("hello");
       this.Events.Add("I'm just doing something");
    }
     
    public void Save(string filename)
    {
    StreamWriter theStreamWriter = new StreamWriter(filename);
    Type[] theExtraTypes = new Type[3];
    theExtraTypes[0] = typeof(ClassA);
    theExtraTypes[1] = typeof(ArrayList);
     
    XmlSerializer serializer = new XmlSerializer(typeof(ArrayList), theExtraTypes);
    serializer.Serialize(theStreamWriter, this.Events);
    theStreamWriter.Close();
    }
     
    public static ClassA Load(string filename)
    {
    StreamWriter theStreamWriter = new StreamWriter(filename);
    XmlSerializer serializer = new XmlSerializer(typeof(ClassA));
     
    ClassA result = (ClassA)serializer.Deserialize(theStreamWriter);
     
    theStreamWriter.Close();
    return (result);
    }
    }

     

     

    Does this help/work for you? Any errors?

    Tuesday, September 26, 2006 9:23 AM

All replies

  • You should just be able to add the Serializable attribute to the class, if you don't need any special control over the serialization, which you don't for a simple type like the ArrayList

    Tuesday, September 26, 2006 8:49 AM
  • indeed, there are no special ways. What errors are you getting if any? you may need to perhaps add an extra type also into the serializer class so it knows what to expect when serializing/deserializing.

    Example I had a class, it contained an ArrayList. This is my class and this is how I would serialize/deserialize it:


    public class ClassA

    {
    private ArrayList Events;
     
    public ClassA()
    {
       this.Events =
    new ArrayList();
       this.Events.Add("hello");
       this.Events.Add("I'm just doing something");
    }
     
    public void Save(string filename)
    {
    StreamWriter theStreamWriter = new StreamWriter(filename);
    Type[] theExtraTypes = new Type[3];
    theExtraTypes[0] = typeof(ClassA);
    theExtraTypes[1] = typeof(ArrayList);
     
    XmlSerializer serializer = new XmlSerializer(typeof(ArrayList), theExtraTypes);
    serializer.Serialize(theStreamWriter, this.Events);
    theStreamWriter.Close();
    }
     
    public static ClassA Load(string filename)
    {
    StreamWriter theStreamWriter = new StreamWriter(filename);
    XmlSerializer serializer = new XmlSerializer(typeof(ClassA));
     
    ClassA result = (ClassA)serializer.Deserialize(theStreamWriter);
     
    theStreamWriter.Close();
    return (result);
    }
    }

     

     

    Does this help/work for you? Any errors?

    Tuesday, September 26, 2006 9:23 AM
  • I found this post helpfull but I encountered a couple of errors, I changed the load method to this and it works perfect for me.

     

    Code Snippet

    public ArrayList Load(string filename)

    {

    StreamReader theStreamReader = new StreamReader(filename);

    Type[] theExtraTypes = new Type[2];

    theExtraTypes[0] = typeof(ClassA);

    theExtraTypes[1] = typeof(ArrayList);

    XmlSerializer serializer = new XmlSerializer(typeof(ArrayList), theExtraTypes);

    ArrayList result = (ArrayList)serializer.Deserialize(theStreamReader);

    theStreamReader.Close();

    return (result);

    }

     

    Wednesday, May 23, 2007 2:17 PM
  • Hi,

    Thank you so much. The key was to register in XmlSerializer class the types to serialize.

    Cheers.

    Monday, September 13, 2010 9:12 AM
  • List can't be serialized as it is, u should use array ( [] ) to serialzie.
    Tuesday, September 14, 2010 3:43 AM
  • You must inform XmlSerializer what types of objects are contained in the ArrayList   

    [XmlElement(typeof(Employee)]
    public ArrayList alEmployees

     

    cf. http://www.diranieh.com/NETSerialization/XMLSerialization.htm


    Tuesday, March 29, 2011 1:56 PM
  • can you just use list<type> instead of ArrayList!

     

    public List<Employee> allEmployees

     

    then it will serialize just fine...

     

    • Proposed as answer by jules345 Tuesday, July 19, 2011 3:00 PM
    Tuesday, July 19, 2011 3:00 PM
  • @jules345 and @karto35

    its works gr8 .... thank you ....  +1 to both ....

    Thursday, April 05, 2012 10:38 AM