2012-07-13 86 views
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; } 
    } 

這些對象的每隨後被存儲在字典(詞典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(); 
    } 

希望有人可以幫助我。

+1

爲什麼SerializableDictionary <字符串,對象>而不是SerializableDictionary ?你必須「幫助」XmlSerializer來完成它的工作。 – 2012-07-13 11:08:13

+1

謝謝你阿德里亞諾,這正是需要的。希望我可以將您的答案標記爲解決方案。 – Rickard 2012-07-13 17:23:36

+0

您不能將評論標記爲解決方案;這是一個真正的答案很短,很樂意幫助! – 2012-07-13 21:09:22

回答

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