• Dictionary 序列化与反序列化


    [转:http://blog.csdn.net/woaixiaozhe/article/details/7873582]

    1.说明:Dictionary对象本身不支持序列化和反序列化,需要定义一个继承自Dictionary, IXmlSerializable类的自定义类来实现该功能。感觉完全可以把这样的类封装到C#库中,很具有通用性嘛,至今没有遇到不能用的情况的说,或许出于其他方面的考虑microsoft才没有这么做。

    2.SerializableDictionary自定义类

     1     [Serializable]
     2     public class SerializableDictionary<TKey, TValue> : Dictionary<TKey, TValue>, IXmlSerializable
     3     {
     4         public SerializableDictionary() { }
     5         public void WriteXml(XmlWriter write)       // Serializer
     6         {
     7             XmlSerializer KeySerializer = new XmlSerializer(typeof(TKey));
     8             XmlSerializer ValueSerializer = new XmlSerializer(typeof(TValue));
     9 
    10             foreach (KeyValuePair<TKey, TValue> kv in this)
    11             {
    12                 write.WriteStartElement("SerializableDictionary");
    13                 write.WriteStartElement("key");
    14                 KeySerializer.Serialize(write, kv.Key);
    15                 write.WriteEndElement();
    16                 write.WriteStartElement("value");
    17                 ValueSerializer.Serialize(write, kv.Value);
    18                 write.WriteEndElement();
    19                 write.WriteEndElement();
    20             }
    21         }
    22         public void ReadXml(XmlReader reader)       // Deserializer
    23         {
    24             reader.Read();
    25             XmlSerializer KeySerializer = new XmlSerializer(typeof(TKey));
    26             XmlSerializer ValueSerializer = new XmlSerializer(typeof(TValue));
    27 
    28             while (reader.NodeType != XmlNodeType.EndElement)
    29             {
    30                 reader.ReadStartElement("SerializableDictionary");
    31                 reader.ReadStartElement("key");
    32                 TKey tk = (TKey)KeySerializer.Deserialize(reader);
    33                 reader.ReadEndElement();
    34                 reader.ReadStartElement("value");
    35                 TValue vl = (TValue)ValueSerializer.Deserialize(reader);
    36                 reader.ReadEndElement();
    37                 reader.ReadEndElement();
    38                 this.Add(tk, vl);
    39                 reader.MoveToContent();
    40             }
    41             reader.ReadEndElement();
    42 
    43         }
    44         public XmlSchema GetSchema()
    45         {
    46             return null;
    47         }
    48     }

    3.使用

      a.定义SerializableDictionary对象,这里以存储<string,string>键对为例:

    1 SerializableDictionary<string, string> serializableDictionary = new SerializableDictionary<string, string>();  

    b.添加元素

    1 serializableDictionary.Add("Key1", “Value1”);  

    c.序列化

    1 using (FileStream fileStream = new FileStream(fileName, FileMode.Create))  
    2 {  
    3     XmlSerializer xmlFormatter = new XmlSerializer(typeof(SerializableDictionary<string, string>));  
    4     xmlFormatter.Serialize(fileStream, this.serializableDictionary);  
    5 }  

     注:文件名fileName自己定义,如“file.xml”

    d.反序列化

    1 using (FileStream fileStream = new FileStream(fileName, FileMode.Open))  
    2 {  
    3     XmlSerializer xmlFormatter = new XmlSerializer(typeof(SerializableDictionary<string, string>));  
    4     this.serializableDictionary = (SerializableDictionary<string,string>)xmlFormatter.Deserialize(fileStream);  
    5 }  
  • 相关阅读:
    第十一次作业
    第十次作业
    第九次作业
    第八次作业
    第七次作业
    第六次作业
    第五次作业
    java第三次作业
    Java第二次作业
    Java第一次作业
  • 原文地址:https://www.cnblogs.com/cpcpc/p/3678785.html
Copyright © 2020-2023  润新知