0

I have an ArrayList which stores a custom object. I want to serialize that ArrayList to a string so I can save it inside the Application settings.

This question looks to resolve it, but is in java. And I am not smart with XML, so could someone help out? Serialize an ArrayList of Date object type

I have my ArrayList setup:

...
MyObject tempObj = new MyObject("something",1,"something");
MyCollection.Add(tempObj);
...

And I originally had this. It outputs the string, but the object isn't there:

    private string SerializeArrayList(ArrayList obj)
    {
            System.Xml.XmlDocument doc = new XmlDocument();
            Type[] extraTypes = new Type[1];
            extraTypes[0] = typeof(MyObject);
            System.Xml.Serialization.XmlSerializer serializer = new System.Xml.Serialization.XmlSerializer(typeof(ArrayList), extraTypes);
            System.IO.MemoryStream stream = new System.IO.MemoryStream();
            try
            {
                serializer.Serialize(stream, obj);
                stream.Position = 0;
                doc.Load(stream);
                return doc.InnerXml;
            }
            catch { throw; }
            finally
            {
                stream.Close();
                stream.Dispose();
            }
}

EDIT: Code request

    public class MyObject
    {
        private string eN;      
        private Boolean bE;          
        private int min;         
        private Boolean bot;       
        private string onE;         


        public MyObject(string na, Boolean b)
        {
          ...
        }


        public MyObject()
        {
        }

        public string GetSomething()
        {
            ...
Community
  • 1
  • 1
JEV
  • 2,494
  • 4
  • 33
  • 47

2 Answers2

5

I tested your code and it seems to work ok, as long as you have [Serializable] on your object.

Also if you are trying to Serialize the fields, you will have to make them public properties.

My Test:

    ArrayList array = new ArrayList();
    Rules tempObj = new Rules { onE = "Value", min = 45, eN = "Value" };
    array.Add(tempObj);
    string result = SerializeArrayList(array);

    private string SerializeArrayList(ArrayList obj)
    {
        XmlDocument doc = new XmlDocument();
        XmlSerializer serializer = new XmlSerializer(typeof(ArrayList), new Type[]{typeof(Rules)});
        using (MemoryStream stream = new System.IO.MemoryStream())
        {
            try
            {
                serializer.Serialize(stream, obj);
                stream.Position = 0;
                doc.Load(stream);
                return doc.InnerXml;
            }
            catch (Exception ex)
            {
            }
        }
        return string.Empty;
    }

Object:

[Serializable]
[XmlType(TypeName = "Rules")]
public class Rules
{
    // Make fields propertys so they will be serialized
    public string eN { get; set; }      //Name
    public Boolean bE { get; set; }     //Whether blocked entirely
    public int min { get; set; }        //Minutes they are allowed if blocked
    public Boolean bot { get; set; }    //Create notification if allowance exceed
    public string onE { get; set; }     //Nothing or CLOSE Process

    public Rules(string na, Boolean b)
    {

    }

    public Rules()
    {
    }
}
sa_ddam213
  • 42,848
  • 7
  • 101
  • 110
  • I did not have that [Serializable] at the top, so will look inot that. Thanks – JEV Feb 04 '13 at 10:33
  • 1
    You are todays winner! Didn't know about having to have the get/setters instead of initializing in constructor. Thanks dude – JEV Feb 04 '13 at 11:19
  • !!MUST declare getters/setters in order for serialization to work! – Steinfeld May 15 '14 at 09:28
3

I ran into a similar problem, and there is this great program called SharpSerializer, which is available through Nuget. It will handle your ArrayList quite easily, just type the code:

 SharpSerializer mySerializer = new SharpSerializer();
 mySerializer.Serialize(ArrayList, "filetosaveto.xml");

Here's the link to the website, its free so don't worry about paying anything:

http://www.sharpserializer.com/en/index.html

David Venegoni
  • 508
  • 3
  • 13
  • I appreciate the answer, however I am looking for a manual solution instead. Given you an upvote though :) – JEV Feb 03 '13 at 20:59
  • I understand. I am hopeful someone who is good with XML will give you some feedback on your problem. If not, you could look into the source code for SharpSerializer and see if that helps you at all. Either way, hope you figure it out. – David Venegoni Feb 03 '13 at 21:43