为什么继承了DbObjectModel后无法进行序列化和反序列化?

May 31, 2010 at 6:50 AM
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Xml.Serialization;
using Lephone.Data.Definition;
namespace ConsoleApplication1
{
    [Serializable]
    public class point : DbObjectModel<point>
    {
        public string point_id { get; set; }
        public string point_name { get; set; }
        public point_type point_type { get; set; }
    }

    [Serializable]
    public class point_type : DbObjectModel<point_type>
    {
        public string type_id { get; set; }
        public string type_name { get; set; }
    }

    [Serializable]
    public class pointList : List<point>
    {
        public void ToXML(string XmlPath)
        {
            string xml = string.Empty;
            //序列化
            using (MemoryStream ms = new MemoryStream())
            {
                XmlSerializer serializer = new XmlSerializer(typeof(pointList));
                serializer.Serialize(ms, this);
                byte[] bs = ms.ToArray();
                xml = Encoding.UTF8.GetString(bs);
            }
            using (System.IO.StreamWriter sw = new StreamWriter(XmlPath))
            {
                sw.Write(xml);
                sw.Flush();
                sw.Close();
            }
        }
        public pointList FromXml(string XmlPath)
        {
            pointList value = null;
            XmlSerializer serializer = new XmlSerializer(typeof(pointList));
            using (FileStream stream = new FileStream(XmlPath, FileMode.Open))
            {
                value = (pointList)serializer.Deserialize(stream);
                stream.Close();
            }
            return value;
        }
    }
    public class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("ToXML...");
            pointList pointList = new pointList();
            pointList.Add(new point() { point_id = "10010", point_name = "A口", point_type = new point_type() { type_id = "10010", type_name = "A" } });
            pointList.Add(new point() { point_id = "10011", point_name = "B口", point_type = new point_type() { type_id = "10011", type_name = "B" } });
            pointList.Add(new point() { point_id = "10012", point_name = "C口", point_type = new point_type() { type_id = "10012", type_name = "C" } });
            pointList.ToXML("c:\\pointList.xml");

            Console.WriteLine("FromXml...");
            var plint = pointList.FromXml("c:\\pointList.xml");
            foreach (var r in plint)
                Console.WriteLine("{0},{1}", r.point_id, r.point_name);
            Console.Read();
        }
    }
}

如上代码,如果继承DbObjectModel,在序列化时就会出错,不继承DbObjectModel运行正常。
point、point_type 是表的映射,这里没有按照abstract的写法进行,原因abstract类无法直接使用,在 pointList.Add 时就会出现问题,所以偷了个懒。
请问这种情况下该如何序列化和反序列化?
Coordinator
May 31, 2010 at 7:05 AM

目前只支持二进制序列化和反序列化,不支持XML序列化和反序列化。

May 31, 2010 at 7:12 AM

OK,谢谢lifeng。

Apr 23, 2012 at 7:37 AM

请问现在的版本能支持到 XML 和 JSON序列化么,能支持到 WCF 的 DataContract 标签么?