zoukankan      html  css  js  c++  java
  • XML Serializable Generic Dictionary

      .net 2.0 泛型Dictionary不支持 XML serializable.  下面是一个实现IXmlSerializable 接口实现支持Serialize的泛型集合.Dictionary 。 Dictionary<TKey, TValue>本身实现了ISerializable接口,WebService中无法实现序列化,具体是什么原因它不支持XML序列化。
      

     1    /// <summary>
     2    /// 支持XML序列化的泛型 Dictionary
     3    /// </summary>
     4    /// <typeparam name="TKey"></typeparam>
     5    /// <typeparam name="TValue"></typeparam>

     6    [XmlRoot("dictionary")]
     7    public class SerializableDictionary<TKey, TValue> 
     8        : Dictionary<TKey, TValue>, IXmlSerializable
     9    {
    10

     #region 构造函数

            public SerializableDictionary():base()
            {

            }
           
            public SerializableDictionary(IDictionary<TKey, TValue> dictionary):base(dictionary)
            {

            }
               
          
            public SerializableDictionary(IEqualityComparer<TKey> comparer):base(comparer)
            {
            }

          
            public SerializableDictionary(int capacity):base(capacity)
            {

            }
          
            public SerializableDictionary(int capacity, IEqualityComparer<TKey> comparer)
                :base(capacity,comparer)
            {

            }

            protected SerializableDictionary(SerializationInfo info, StreamingContext context):base(info,context)
            {

            }

            #endregion
    11        #region IXmlSerializable Members 
    12        public System.Xml.Schema.XmlSchema GetSchema()
    13        {   
    14            return null;   
    15        }
         
    16
    17        /// <summary>
    18        /// 从对象的 XML 表示形式生成该对象
    19        /// </summary>
    20        /// <param name="reader"></param>

    21        public void ReadXml(System.Xml.XmlReader reader)
    22        {
    23
    24            XmlSerializer keySerializer = new XmlSerializer(typeof(TKey));
    25            XmlSerializer valueSerializer = new XmlSerializer(typeof(TValue)); 
    26            bool wasEmpty = reader.IsEmptyElement;   
    27            reader.Read();     
    28
    29            if (wasEmpty)   
    30                return;     
    31            while (reader.NodeType != System.Xml.XmlNodeType.EndElement)     
    32            {   
    33                reader.ReadStartElement("item");  
    34                reader.ReadStartElement("key");   
    35                TKey key = (TKey)keySerializer.Deserialize(reader);  
    36                reader.ReadEndElement();   
    37                reader.ReadStartElement("value");  
    38                TValue value = (TValue)valueSerializer.Deserialize(reader); 
    39                reader.ReadEndElement();   
    40                this.Add(key, value); 
    41                reader.ReadEndElement();
    42                reader.MoveToContent();
    43
    44            }
                 
    45            reader.ReadEndElement();
    46
    47        }

    48
    49
    50        /// <summary>
    51        /// 将对象转换为其 XML 表示形式
    52        /// </summary>
    53        /// <param name="writer"></param>

    54        public void WriteXml(System.Xml.XmlWriter writer)
    55        {
    56
    57            XmlSerializer keySerializer = new XmlSerializer(typeof(TKey)); 
    58            XmlSerializer valueSerializer = new XmlSerializer(typeof(TValue)); 
    59            foreach (TKey key in this.Keys)
    60            {  
    61                writer.WriteStartElement("item");
    62                writer.WriteStartElement("key"); 
    63                keySerializer.Serialize(writer, key);  
    64                writer.WriteEndElement();      
    65                writer.WriteStartElement("value");
    66                TValue value = this[key];     
    67                valueSerializer.Serialize(writer, value);   
    68                writer.WriteEndElement();    
    69                writer.WriteEndElement();
    70            }

    71
    72        }

    73
    74        #endregion

    75
    76    }

  • 相关阅读:
    Oracle序列使用:建立、删除
    struts1.x入门
    SQL的四种连接-左外连接、右外连接、内连接、全连接
    eclipse更改文件编码方式
    使用links方式安装Eclipse插件
    JAVA:Eclipse代码自动提示
    MyEclipse注释配置
    全面理解SQL
    一秒去除Win7快捷方式箭头
    Eclipse快捷键大全(转载)
  • 原文地址:https://www.cnblogs.com/zzh/p/1743524.html
Copyright © 2011-2022 走看看