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()
        {
            ...
9
  • Could you add the code for MyObject? Also, could you add the result you are getting and the result you are expecting? Commented Feb 3, 2013 at 18:08
  • What do you mean by "It outputs the string, but the object isn't there"? You are trying to serialize to string right? Commented Feb 3, 2013 at 18:12
  • Yes I am calling it like string tmp = SerializeArrayList(MyCollection); will edit main. Commented Feb 3, 2013 at 19:32
  • 1Done the edits! Please check main Commented Feb 3, 2013 at 19:35
  • Have you marked your custom object's class as serializable? Commented Feb 3, 2013 at 19:44

2 Answers 2

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()
    {
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

I did not have that [Serializable] at the top, so will look inot that. Thanks
You are todays winner! Didn't know about having to have the get/setters instead of initializing in constructor. Thanks dude
!!MUST declare getters/setters in order for serialization to work!
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

2 Comments

I appreciate the answer, however I am looking for a manual solution instead. Given you an upvote though :)
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.