2 Replies - 736 Views - Last Post: 24 April 2011 - 09:19 AM Rate Topic: -----

#1 skybomb0  Icon User is offline

  • D.I.C Head

Reputation: 10
  • View blog
  • Posts: 92
  • Joined: 12-May 10

Serialize Auto Property

Posted 23 April 2011 - 11:21 PM

I am making a class to represent a stack of flash cards. The Stack class is a list which implements ICollection, therefore, it is serializable. And the list of cards serializes fine. I thought serialization automatically serialized public properties. So why isn't it serializing the Name property in my Stack class?

    public class Stack : List<Card>
    {
        static void Main()
        {
            Stack s = new Stack();
            s.Add(new Card("Q1", "A1"));
            s.Add(new Card("Q2", "A2"));

            s.Save(@"C:\Users\Danny\Desktop\output.xml");
        }



        public string Name { get; set; } // Shouldn't this serialize???

        public void Save(string path)
        {
            XmlSerializer serializer = new XmlSerializer(typeof(Stack));
            StreamWriter writer = new StreamWriter(path);

            try
            {
                serializer.Serialize(writer, this);
            }
            finally
            {
                writer.Close();
            }
        }
    }

    public class Card
    {
        public string Question { get; set; }
        public string Answer { get; set; }

        public Card() : this("", "") { }
        public Card(string question, string answer)
        {
            this.Question = question;
            this.Answer = answer;
        }
    }


This post has been edited by skybomb0: 23 April 2011 - 11:24 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Serialize Auto Property

#2 CodingSup3rnatur@l-360  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 991
  • View blog
  • Posts: 971
  • Joined: 30-September 10

Re: Serialize Auto Property

Posted 24 April 2011 - 02:48 AM

Hi,

The thing to recognise here is that it is not the automatic property that is the problem.

The fact that you have inherited from List<Card> means that the Stack class (you should choose a different name by the way, or use the built in Stack collection class :)) is treated as a collection. It IS-A collection ONLY, NOT a class that can have other fields or properties (as far as Xml serialization is concerned at least).

Therefore, only the collection is serialized. Try putting a field, or a normal property in the class also. You will see that they are not serialized either

You may notice that in the XML produced, there will be an element call '<ArrayOfCard>', or something similar. This shows that the class is being treated as an array, i.e. a collection ONLY.


To get around this, you can wrap a generic List with your Stack class (i.e, put a list as a property in your class to hold the cards, rather than inheriting from List<T> directly).

Or you could implement IXmlSerializable if you really want to keep inheriting from List<T>. Then you get to choose exactly what gets serialized :)

This post has been edited by CodingSup3rnatur@l-360: 24 April 2011 - 03:37 AM

Was This Post Helpful? 4
  • +
  • -

#3 skybomb0  Icon User is offline

  • D.I.C Head

Reputation: 10
  • View blog
  • Posts: 92
  • Joined: 12-May 10

Re: Serialize Auto Property

Posted 24 April 2011 - 09:19 AM

OH. I got it now. That makes sense. It works now. +1 for you.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1