1

在我的程序中,我将一些数据保存在自定义对象中:

    [Serializable]
    [XmlInclude(typeof(MsgTimerSettings))]
    public class MsgTimerSettings
    {
        public string t_name { get; set; }
        public string t_subj { get; set; }
        public string t_nmsg { get; set; }
        public UUID t_attach { get; set; }
        public string t_attachName { get; set; }
        public Dictionary<string, UUID> t_ngroups { get; set; }
        public Dictionary<string, UUID> t_imgroups { get; set; }
        public string t_IMmsg { get; set; }
        public string t_SLURL { get; set; }
        public int t_Type { get; set; }
        public int t_IMsetting { get; set; }
        public int t_ImgIndex { get; set; }
        public bool t_StartNow { get; set; }
        public DateTime t_StartTime { get; set; }
        public bool t_EndAt { get; set; }
        public DateTime t_EndTime { get; set; }
        public int t_Number { get; set; }
        public string t_Period { get; set; }
        public int t_Interval { get; set; }
        public bool t_Active { get; set; }
    }

然后将这些对象中的每一个存储在 Dictionary(Dictionary MsgTimers;) 中,字典键是计时器的名称。

现在,我想做的是将这些对象中的每一个保存到一个 XML 文件中,以便下次启动程序时可以读回计时器。

我知道字典不可序列化,我试图找到不同的解决方案来解决这个问题,但无济于事。

我尝试使用在以下位置提供的 SerializableDictionary 解决方法:http ://weblogs.asp.net/pwelter34/archive/2006/05/03/444961.aspx以制作可序列化的字典,但不仅仅是将我的字典制作成 SerializableDictionaries ,我还是没有头绪...

我尝试序列化的代码如下:

    static public void SerializeToXML(SerializableDictionary<string, object> ST)
    {
        XmlSerializer serializer = new XmlSerializer(typeof(SerializableDictionary<string,object>));
        TextWriter textWriter = new StreamWriter(@"C:\ST.xml");
        serializer.Serialize(textWriter, ST);
        textWriter.Close();
    }

希望任何人都可以帮助我。

4

1 回答 1

0

要在不引入可序列化字典的情况下序列化/反序列化字典,我使用这个类:

public class DictionarySerializer : IXmlSerializable
{
    private Dictionary<string, object> _dictionary;

    private DictionarySerializer()
    {
        this._dictionary = new Dictionary<string,object>();
    }

    private DictionarySerializer(Dictionary<string, object> dictionary)
    {
        this._dictionary = dictionary;
    }

    public static void Serialize(StreamWriter stream, Dictionary<string, object> dictionary)
    {
        DictionarySerializer ds = new DictionarySerializer(dictionary);
        XmlSerializer xs = new XmlSerializer(typeof(DictionarySerializer));
        xs.Serialize(stream, ds);
    }

    public static void Serialize(Stream stream, Dictionary<string, object> dictionary)
    {
        DictionarySerializer ds = new DictionarySerializer(dictionary);
        XmlSerializer xs = new XmlSerializer(typeof(DictionarySerializer));
        xs.Serialize(stream, ds);
    }

    public static Dictionary<string, object> Deserialize(Stream stream)
    {
        XmlSerializer xs = new XmlSerializer(typeof(DictionarySerializer));
        DictionarySerializer ds = (DictionarySerializer)xs.Deserialize(stream);
        return ds._dictionary;
    }

    public static Dictionary<string, object> Deserialize(TextReader stream)
    {
        XmlSerializer xs = new XmlSerializer(typeof(DictionarySerializer));
        DictionarySerializer ds = (DictionarySerializer)xs.Deserialize(stream);
        return ds._dictionary;
    }

    XmlSchema IXmlSerializable.GetSchema()
    {
        return null;
    }

    void IXmlSerializable.ReadXml(XmlReader reader)
    {
        reader.Read();
        ReadFromXml(reader);
    }

    private void ReadFromXml(XmlReader reader)
    {
        reader.ReadStartElement("dictionary");
        while (reader.NodeType != XmlNodeType.EndElement)
        {
            reader.ReadStartElement("item");
            string key = reader.ReadElementString("key");
            reader.ReadStartElement("value");
            object value = null;
            if (reader.Name == string.Empty)
            {
                value = reader.Value;
                reader.Read();
            }
            else if (reader.Name == "dictionary")
            {
                DictionarySerializer innerSerializer = new DictionarySerializer();
                innerSerializer.ReadFromXml(reader);
                value = innerSerializer._dictionary;
            }
            reader.ReadEndElement();
            reader.ReadEndElement();
            reader.MoveToContent();
            _dictionary.Add(key, value);
        }
        reader.ReadEndElement();
    }


    void IXmlSerializable.WriteXml(XmlWriter writer)
    {
        writer.WriteStartElement("dictionary");
        foreach (string key in _dictionary.Keys)
        {
            object value = _dictionary[key];
            writer.WriteStartElement("item");
            writer.WriteElementString("key", key.ToString());
            if (value is Dictionary<string, object>)
            {
                writer.WriteStartElement("value");
                IXmlSerializable aSer = new DictionarySerializer((Dictionary<string, object>)value);
                aSer.WriteXml(writer);
                writer.WriteEndElement();
            }
            else
                writer.WriteElementString("value", value.ToString());
            writer.WriteEndElement();
        }
        writer.WriteEndElement();
    }
}

然后我使用这种代码来序列化/反序列化字典:

        Dictionary<string, object> d = new Dictionary<string, object>();
        // fill the dictionary
        StreamWriter sw = new StreamWriter(pth);
        DictionarySerializer.Serialize(sw, d);
        sw.Close();

        StreamReader sr = new StreamReader(pth);
        Dictionary<string, object> d2 = DictionarySerializer.Deserialize(sr);
        sr.Close();
于 2012-07-13T12:33:48.500 回答