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();
}
希望有人可以幫助我。
爲什麼SerializableDictionary <字符串,對象>而不是SerializableDictionary?你必須「幫助」XmlSerializer來完成它的工作。 –
2012-07-13 11:08:13
謝謝你阿德里亞諾,這正是需要的。希望我可以將您的答案標記爲解決方案。 – Rickard 2012-07-13 17:23:36
您不能將評論標記爲解決方案;這是一個真正的答案很短,很樂意幫助! – 2012-07-13 21:09:22