title: XML Serialisation question
body: <p>I'm trying to teach-myself how to serialize/deserialize to/from XML using the attribute-based serializer. I've put the below code together for testing purposes but it seems that I might have missed a point or two. Can anyone assist me and tell me what to do to have the whole damn thing work properly? The code below should compile and run just fine but will throw an exception - something is probably wrong with my attributes. </p>
  
  <p>What did I miss? </p>
  
  <p><strong>Program.cs</strong></p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Text;
  using System.Runtime.Serialization;
  using System.IO;
  
  namespace XMLSerialisation_test
  {
      class Program
      {
          static void Main(string[] args)
          {
              World my_world = new World(new Point(20, 30));
  
              for (int i = 0; i &lt; 10; i++)
              {
                  string property = String.Format("Property no.{0}", i);
                  my_world.PushWorldObject(new MyObject(new Point(i, i), property));
              }
  
              DataContractSerializer world_serializer = new DataContractSerializer(typeof(World));
  
              try
              {
                  using (Stream s = File.Create("output.xml"))
                      world_serializer.WriteObject(s, my_world);
              }
              catch (Exception e)
              {
                  Console.WriteLine("Exception occured : {0}", e.Message);
              }
  
          }
      }
  }
  </code></pre>
  
  <p><strong>WorldObject.cs</strong></p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Text;
  using System.Runtime.Serialization;
  
  namespace XMLSerialisation_test
  {
      [DataContract]
      public struct Point // couldn't find the pre-defined Point for some reason
      {
          public Point(double x, double y)
          { X = x; Y = y; }
          [DataMember]
          public double X;
          [DataMember]
          public double Y;
      }
  
      [DataContract]
      public abstract class WorldObject
      {
          public WorldObject() : this(0.0, 0.0)
          {}
  
          public WorldObject(Point loc)
          { m_location = loc; }
  
          public WorldObject(double x, double y)
          { 
              m_location.X = x;
              m_location.Y = y;
          }
  
          [DataMember]
          public Point Location
          {
              get { return m_location; }
              set { m_location = value; }
          }
  
          protected Point m_location;
      }
  
      [DataContract]
      public class MyObject : WorldObject
      {
          public MyObject(string prop)
              : base(0.0, 0.0)
          { m_property = prop; }
  
          public MyObject(Point p, string prop)
              : base(p)
          { m_property = prop; }
  
          [DataMember]
          public string Property
          {
              get{ return m_property; }
              set{ m_property = value; }
          }
  
          private string m_property;
      }
  }
  </code></pre>
  
  <p><strong>World.cs</strong></p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Text;
  using System.Runtime.Serialization;
  using System.Xml.Serialization;
  
  namespace XMLSerialisation_test
  {
      [DataContract]
      class World
      {
          public World() : this(new Point(10, 10))
          { }
  
          public World(Point size)
          { m_world_size = size; }
  
          public bool PushWorldObject(WorldObject o)
          {
              try
              {
                  WorldObjects.Add(o);
                  return true;
              }
              catch(Exception e)
              {
                  Console.WriteLine("Exception occured : {0}", e.Message);
                  return false; }
          }
  
  
          #region Accessors
          [DataMember]
          public List&lt;WorldObject&gt; WorldObjects
          {
              get { return m_world_objects; }
              set { m_world_objects = value; }
          }
          [DataMember]
          public Point WorldSize
          {
              get { return m_world_size; }
              private set { m_world_size = value; }
          }
          #endregion
  
          #region Fields
          private List&lt;WorldObject&gt; m_world_objects = new List&lt;WorldObject&gt;();
          private Point m_world_size;
          #endregion
      }
  }
  </code></pre>