C# 序列化包含自定义对象(包括两个字典)的字典
在我的程序中,我将一些数据保存在自定义对象中:C# 序列化包含自定义对象(包括两个字典)的字典,c#,xml,xml-serialization,C#,Xml,Xml Serialization,在我的程序中,我将一些数据保存在自定义对象中: [Serializable] [XmlInclude(typeof(MsgTimerSettings))] public class MsgTimerSettings { public string t_name { get; set; } public string t_subj { get; set; } public string t_nmsg { get; se
[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; }
}
[可序列化]
[xmlclude(typeof(MsgTimerSettings))]
公共类MsgTimerSettings
{
公共字符串t_name{get;set;}
公共字符串t_subj{get;set;}
公共字符串t_nmsg{get;set;}
公共UUID t_attach{get;set;}
公共字符串t_attachName{get;set;}
公共字典组{get;set;}
公共字典t_imgroups{get;set;}
公共字符串t_IMmsg{get;set;}
公共字符串t_SLURL{get;set;}
公共int t_类型{get;set;}
公共int t_IMsetting{get;set;}
公共整数t_ImgIndex{get;set;}
公共bool t_StartNow{get;set;}
公共日期时间t_开始时间{get;set;}
公共bool t_EndAt{get;set;}
公共日期时间t_EndTime{get;set;}
公共整数t_数{get;set;}
公共字符串t_句点{get;set;}
公共整数t_区间{get;set;}
公共bool t_Active{get;set;}
}
然后,这些对象中的每一个都存储在字典(Dictionary MsgTimers;)
字典键是计时器的名称
现在,我想做的是将这些对象保存到一个XML文件中,以便下次启动程序时可以读回计时器
我知道字典是不可序列化的,我也曾试图找到不同的解决方案来解决这个问题,但都无济于事
我曾尝试使用SerializableDictionary解决方法,介绍于:以制作可序列化词典,但除了将我的词典制作成可序列化词典之外,我仍然没有任何线索
我尝试序列化的代码如下所示:
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();
}
静态公共void SerializeToXML(SerializableDictionary ST)
{
XmlSerializer serializer=新的XmlSerializer(typeof(SerializableDictionary));
TextWriter TextWriter=newstreamwriter(@“C:\ST.xml”);
serializer.Serialize(textWriter,ST);
textWriter.Close();
}
希望任何人都能帮助我。要在不引入可序列化词典的情况下序列化/反序列化词典,我使用以下类:
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();
}
}
公共类字典序列化器:IXmlSerializable
{
私人词典;
专用字典序列化程序()
{
这本字典=新字典();
}
专用字典序列化器(字典字典)
{
这本字典=字典;
}
公共静态void序列化(StreamWriter流、字典)
{
DictionarySerializer ds=新DictionarySerializer(字典);
XmlSerializer xs=新的XmlSerializer(typeof(DictionarySerializer));
序列化(流,ds);
}
公共静态void序列化(流、字典)
{
DictionarySerializer ds=新DictionarySerializer(字典);
XmlSerializer xs=新的XmlSerializer(typeof(DictionarySerializer));
序列化(流,ds);
}
公共静态字典反序列化(流)
{
XmlSerializer xs=新的XmlSerializer(typeof(DictionarySerializer));
DictionarySerializer ds=(DictionarySerializer)xs.反序列化(流);
返回ds.\U字典;
}
公共静态字典反序列化(TextReader流)
{
XmlSerializer xs=新的XmlSerializer(typeof(DictionarySerializer));
DictionarySerializer ds=(DictionarySerializer)xs.反序列化(流);
返回ds.\U字典;
}
XmlSchema IXmlSerializable.GetSchema()
{
返回null;
}
void IXmlSerializable.ReadXml(XmlReader)
{
reader.Read();
ReadFromXml(阅读器);
}
私有void ReadFromXml(XmlReader)
{
reader.ReadStartElement(“字典”);
while(reader.NodeType!=XmlNodeType.EndElement)
{
reader.ReadStartElement(“项目”);
string key=reader.ReadElementString(“key”);
reader.ReadStartElement(“价值”);
对象值=空;
if(reader.Name==string.Empty)
{
value=reader.value;
reader.Read();
}
else if(reader.Name==“dictionary”)
{
DictionarySerializer innerSerializer=新DictionarySerializer();
innerSerializer.ReadFromXml(读取器);
值=innerSerializer.\u字典;
}
reader.ReadEndElement();
reader.ReadEndElement();
reader.MoveToContent();
_添加(键、值);
}
reader.ReadEndElement();
}
void IXmlSerializable.WriteXml(XmlWriter编写器)
{
writer.writeStarElement(“字典”);
foreach(字典中的字符串键.Keys)
{
对象值=_字典[键];
编写人。书面启动项(“项目”);
WriteElementString(“key”,key.ToString());
if(值为字典)
{
writer.writeStarteElement(“值”);
IXmlSerializable aSer=新字典序列化器((字典)值);
aSer.WriteXml(writer);
writer.writeedelement();
}
其他的
writer.WriteElementString(“value”,value.ToString());
writer.writeedelement();
}
writer.writeedelement();
}
}
然后我使用这种代码对字典进行序列化/反序列化:
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();
Dictionary d=newdictionary();
//查字典
StreamWriter sw=新StreamWriter(pth);
字典序列化器。序列化(sw,d);
sw.Close();
StreamReader sr=新StreamReader(pth);
Dictionary d2=DictionarySerializer.Deserialize(sr);