惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Jina AI
Jina AI
V
Vulnerabilities – Threatpost
Security Latest
Security Latest
AI
AI
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
量子位
H
Help Net Security
Attack and Defense Labs
Attack and Defense Labs
The GitHub Blog
The GitHub Blog
L
LINUX DO - 最新话题
A
Arctic Wolf
博客园_首页
S
Securelist
S
Secure Thoughts
Google DeepMind News
Google DeepMind News
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
T
Tailwind CSS Blog
Apple Machine Learning Research
Apple Machine Learning Research
酷 壳 – CoolShell
酷 壳 – CoolShell
Stack Overflow Blog
Stack Overflow Blog
N
Netflix TechBlog - Medium
Cyberwarzone
Cyberwarzone
小众软件
小众软件
T
Threatpost
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
Blog — PlanetScale
Blog — PlanetScale
N
News and Events Feed by Topic
NISL@THU
NISL@THU
Forbes - Security
Forbes - Security
博客园 - 聂微东
F
Fortinet All Blogs
Simon Willison's Weblog
Simon Willison's Weblog
H
Heimdal Security Blog
罗磊的独立博客
S
Security @ Cisco Blogs
B
Blog
T
Troy Hunt's Blog
Engineering at Meta
Engineering at Meta
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
The Hacker News
The Hacker News
The Last Watchdog
The Last Watchdog
Hacker News - Newest:
Hacker News - Newest: "LLM"
I
Intezer
T
Threat Research - Cisco Blogs
C
Cybersecurity and Infrastructure Security Agency CISA
The Cloudflare Blog
S
Schneier on Security
月光博客
月光博客
L
LINUX DO - 热门话题
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org

博客园 - zzh

Making IE use PNG Alpha transparency 关于div高度自适应问题,兼容IE和火狐 C#利用短信猫收发短信息的方法 String[3]: the Size property has an invalid size of 0. - zzh C#中的多线程——线程同步基础 安装window service 中出现Set Service Login对话框 input style兼容IE6的方案 - zzh - 博客园 document.body.scrollTop为0的处理办法 js 获取浏览器高度和宽度值(多浏览器) Assembly Manifest 通俗简易手册 C# Enumeration 使用 关于页面的已终止操作 - zzh - 博客园 DOM标准与IE的html元素事件模型区别 访问ASP.NET临时文件夹的权限问题 打架必备!擒敌拳1-16动连贯动作 分解动作 Ajax错误提示Sys未定义解决方法 - zzh - 博客园 IE上关于PNG做背景,层上超链接不能点击的解决办法 - zzh - 博客园 解决js中onMouseOut事件冒泡的问题 javascript 中的this 与事件注册 及event
XML Serializable Generic Dictionary
zzh · 2010-05-25 · via 博客园 - zzh

  .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    }