自由、创新、研究、探索……

Linux/Windows Mono/DotNet [ Open Source .NET Development/ 使用开源工具进行DotNet软件开发]
posts - 514, comments - 1977, trackbacks - 141, articles - 55
  博客园 :: 首页 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理
    .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        IXmlSerializable Members
75
76    }

自由、创新、研究、探索……
Url: http://shanyou.cnblogs.com
website: http://www.openbeta.cn

Feedback

#1楼    回复  引用  查看    

2006-05-30 20:26 by 毛毛      
非常感谢

#2楼    回复  引用    

2006-06-27 09:16 by Bao*3 [未注册用户]
很有用

#3楼    回复  引用    

2007-02-03 07:41 by 梦幻天涯 [未注册用户]
谢谢,我系统中还真出现了这类问题,刚好用您的方法解决。

#4楼    回复  引用  查看    

2007-02-20 21:52 by JesseZhao      
和我一样,系统中也出现了,解决方法一样

#5楼    回复  引用    

2007-08-25 16:17 by skytouch [未注册用户]
为什么序列化、反序列化这样的类没有问题
class test
{
SerializableDictionary<int,int> dicDAta;
}
但是直接创建SerializableDictionary后反序列化就出问题了
SerializableDictionary<int,int>test=new SerializableDictionary<int,int>();