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