zoukankan      html  css  js  c++  java
  • Dictionary(支持 XML 序列化),注意C#中原生的Dictionary类是无法进行Xml序列化的

     1     /// <summary>
     2     /// Dictionary(支持 XML 序列化)
     3     /// </summary>
     4     /// <typeparam name="TKey">键类型</typeparam>
     5     /// <typeparam name="TValue">值类型</typeparam>
     6     [XmlRoot("XmlDictionary")]
     7     [Serializable]
     8     public class XmlDictionary<TKey, TValue> : Dictionary<TKey, TValue>, IXmlSerializable
     9     {
    10         #region 构造函数
    11         public XmlDictionary()
    12         { }
    13 
    14         public XmlDictionary(IDictionary<TKey, TValue> dictionary) : base(dictionary)
    15         { }
    16 
    17         public XmlDictionary(IEqualityComparer<TKey> comparer) : base(comparer)
    18         { }
    19 
    20         public XmlDictionary(int capacity) : base(capacity)
    21         { }
    22 
    23         public XmlDictionary(int capacity, IEqualityComparer<TKey> comparer) : base(capacity, comparer)
    24         { }
    25 
    26         protected XmlDictionary(SerializationInfo info, StreamingContext context) : base(info, context)
    27         { }
    28         #endregion 构造函数
    29 
    30         #region IXmlSerializable Members
    31         public XmlSchema GetSchema() => null;
    32 
    33         /// <summary>
    34         ///     从对象的 XML 表示形式生成该对象(反序列化)
    35         /// </summary>
    36         /// <param name="xr"></param>
    37         public void ReadXml(XmlReader xr)
    38         {
    39             if (xr.IsEmptyElement)
    40                 return;
    41             var ks = new XmlSerializer(typeof(TKey));
    42             var vs = new XmlSerializer(typeof(TValue));
    43             xr.Read();
    44             while (xr.NodeType != XmlNodeType.EndElement)
    45             {
    46                 xr.ReadStartElement("Item");
    47                 xr.ReadStartElement("Key");
    48                 var key = (TKey)ks.Deserialize(xr);
    49                 xr.ReadEndElement();
    50                 xr.ReadStartElement("Value");
    51                 var value = (TValue)vs.Deserialize(xr);
    52                 xr.ReadEndElement();
    53                 Add(key, value);
    54                 xr.ReadEndElement();
    55                 xr.MoveToContent();
    56             }
    57             xr.ReadEndElement();
    58         }
    59 
    60         /// <summary>
    61         ///     将对象转换为其 XML 表示形式(序列化)
    62         /// </summary>
    63         /// <param name="xw"></param>
    64         public void WriteXml(XmlWriter xw)
    65         {
    66             var ks = new XmlSerializer(typeof(TKey));
    67             var vs = new XmlSerializer(typeof(TValue));
    68             foreach (var key in Keys)
    69             {
    70                 xw.WriteStartElement("Item");
    71                 xw.WriteStartElement("Key");
    72                 ks.Serialize(xw, key);
    73                 xw.WriteEndElement();
    74                 xw.WriteStartElement("Value");
    75                 vs.Serialize(xw, this[key]);
    76                 xw.WriteEndElement();
    77                 xw.WriteEndElement();
    78             }
    79         }
    80         #endregion IXmlSerializable Members
    81     }
    View Code
  • 相关阅读:
    73. Set Matrix Zeroes
    289. Game of Live
    212. Word Search II
    79. Word Search
    142. Linked List Cycle II
    141. Linked List Cycle
    287. Find the Duplicate Number
    260. Single Number III
    137. Single Number II
    Oracle EBS中有关Form的触发器的执行顺序
  • 原文地址:https://www.cnblogs.com/maozhh/p/6731672.html
Copyright © 2011-2022 走看看