开发者

XML Serialisation question

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.

What did I miss?

Program.cs

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 < 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);
            }

        }
    }
}

WorldObject.cs

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;
    }
}

World.cs

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)
            开发者_如何学C{
                Console.WriteLine("Exception occured : {0}", e.Message);
                return false; }
        }


        #region Accessors
        [DataMember]
        public List<WorldObject> 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<WorldObject> m_world_objects = new List<WorldObject>();
        private Point m_world_size;
        #endregion
    }
}


Try this:

DataContractSerializer world_serializer = new DataContractSerializer(typeof(World), new List<Type> { typeof(MyObject) });

The problem is that PushWorldObject takes type WorldObject, but you are actually passing type MyObject. The serializer knows nothing about this type, so throws an exception. WorldObject is used within the World class, so this type is by known by default. However, MyObject is not used inside World - so you manually have to declare it as Known.

As an alternative, you can add the KnownType attribute like so:

[DataContract, KnownType(typeof(MyObject))]
class World
{


The biggest problem I see is the abstract WorldObject. You'll need to use a KnownType attribute there (or via config or method name- check MSDN for "Data Contract Known Types") to tell it about the known subtypes it might encounter (eg, MyObject).


Just to clarify: if you are serializing to xml for the purposes of having xml in a known format, then DataContractSerializer is a bad choice; you would do better to use XmlSerializer, which has more control over xml layout. You would use the [XmlElement] / [XmlAttribute] / [XmlType] / [XmlRoot] attributes instead of [DataMember] etc.

If you just want to use xml because it is text and stores the object, then DataContractSerializer is fine; and has other advantages, for example - not demanding a public parameterless constructor (which XmlSerializer does), and working with private members (which XmlSerializer doesn't).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜