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

推荐订阅源

C
CERT Recently Published Vulnerability Notes
U
Unit 42
T
The Blog of Author Tim Ferriss
H
Hackread – Cybersecurity News, Data Breaches, AI and More
B
Blog RSS Feed
Microsoft Azure Blog
Microsoft Azure Blog
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
S
Securelist
L
Lohrmann on Cybersecurity
Blog — PlanetScale
Blog — PlanetScale
Recorded Future
Recorded Future
D
DataBreaches.Net
Spread Privacy
Spread Privacy
T
Threat Research - Cisco Blogs
I
Intezer
P
Palo Alto Networks Blog
Simon Willison's Weblog
Simon Willison's Weblog
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
I
InfoQ
宝玉的分享
宝玉的分享
Security Latest
Security Latest
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
T
Threatpost
Cisco Talos Blog
Cisco Talos Blog
P
Proofpoint News Feed
博客园 - 司徒正美
H
Hacker News: Front Page
Y
Y Combinator Blog
爱范儿
爱范儿
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
NISL@THU
NISL@THU
月光博客
月光博客
有赞技术团队
有赞技术团队
Cloudbric
Cloudbric
酷 壳 – CoolShell
酷 壳 – CoolShell
G
Google Developers Blog
A
Arctic Wolf
博客园 - 【当耐特】
W
WeLiveSecurity
V
Visual Studio Blog
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
V
V2EX
C
Cyber Attacks, Cyber Crime and Cyber Security
S
SegmentFault 最新的问题
The GitHub Blog
The GitHub Blog
The Cloudflare Blog
Stack Overflow Blog
Stack Overflow Blog

博客园 - 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    }