【问题标题】:Serializing a custom class to XML将自定义类序列化为 XML
【发布时间】:2012-02-17 01:03:14
【问题描述】:

我有一个包含SortedList<string, Data> 作为私有字段的类,其中Data 是一个简单的自定义类,其中包含一些intDateTimeNullable<DateTime> 字段。

public class CustomCollection
{
    private SortedList<string, Data> _list;

    ...
}

现在我将让我的类可序列化,这样我就可以将其内容(即_list 字段的项)写入 XML 文件或从现有 XML 文件加载数据。

我应该如何进行?

我想我理解有两种方法可以序列化:第一种是将所有字段标记为可序列化,而第二种是实现IXmlSerializable 接口。如果我理解正确,我什么时候可以使用这两种方式?

【问题讨论】:

  • 您是说要从 xml 文件中加载 _list 的 xml 表示形式并将其保存到 xml 文件中吗?
  • @Ricky.G:是的,加载和保存到一个XML文件。
  • msdn.microsoft.com/en-us/magazine/cc164135.aspx 可能会有所帮助。有时间我会为你写一些测试源码。
  • @findcaiyzh:为清晰有趣的文章+1!

标签: c# .net serialization xml-serialization


【解决方案1】:

好的,你只需要用 [Serializable] 属性装饰你的类,它应该可以工作。但是,您有一个实现 IDictionary 的 SortedList 并且这些不能使用 IXMLSerializable 序列化,因此需要在此处进行一些自定义查看

Serializing .NET dictionary

但如果您将排序列表更改为普通列表或任何未实现 IDictionary 的内容,则以下代码将起作用 :-) 将其复制到控制台应用程序并运行它。

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Data d = new Data { CurrentDateTime = DateTime.Now, DataId = 1 };
            Data d1 = new Data { CurrentDateTime = DateTime.Now, DataId = 2 };
            Data d2 = new Data { CurrentDateTime = DateTime.Now, DataId = 3 };

            CustomCollection cc = new CustomCollection
                                      {List = new List<Data> {d, d1, d2}};

            //This is the xml
            string xml = MessageSerializer<CustomCollection>.Serialize(cc);

            //This is deserialising it back to the original collection
            CustomCollection collection = MessageSerializer<CustomCollection>.Deserialize(xml);
        }
    }

    [Serializable]
    public class Data
    {
        public int DataId;
        public DateTime CurrentDateTime;
        public DateTime? CurrentNullableDateTime;
    }

    [Serializable]
    public class CustomCollection
    {
        public List<Data> List;
    }

    public class MessageSerializer<T>
    {
        public static T Deserialize(string type)
        {
            var serializer = new XmlSerializer(typeof(T));

            var result = (T)serializer.Deserialize(new StringReader(type));

            return result;
        }

        public static string Serialize(T type)
        {
            var serializer = new XmlSerializer(typeof(T));
            string originalMessage;

            using (var ms = new MemoryStream())
            {
                serializer.Serialize(ms, type);
                ms.Position = 0;
                var document = new XmlDocument();
                document.Load(ms);

                originalMessage = document.OuterXml;
            }

            return originalMessage;
        }
    }
}

【讨论】:

  • 我认为顺序读取 SortedList 中所有元素的一种方法是使用 foreach(KeyValuePair&lt;string, Data&gt; kvp in _list) { // serialize an element } 逐一枚举它们
  • 您可以实现自己的自定义字典,扩展现有字典并且可序列化,看看这里weblogs.asp.net/pwelter34/archive/2006/05/03/444961.aspx
猜你喜欢
  • 2011-11-03
  • 2010-12-20
  • 1970-01-01
  • 2012-06-08
  • 1970-01-01
  • 2012-02-09
  • 2014-05-02
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多