C# Dictionary的xml序列化

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.IO;
 6 using System.Xml;
 7 using System.Xml.Schema;
 8 using System.Xml.Serialization;
 9 namespace SerialDict
10 {
11     [Serializable]
12     public class SerialDictionary<TKey, TValue> : Dictionary<TKey, TValue>, IXmlSerializable
13     {
14         private string name;
15         public SerialDictionary()
16         {
17             this.name = "SerialDictionary";
18         }
19         public SerialDictionary(string name)
20         {
21             this.name = name;
22         }
23         public void WriteXml(XmlWriter write)       // Serializer
24         {
25             XmlSerializer KeySerializer = new XmlSerializer(typeof(TKey));
26             XmlSerializer ValueSerializer = new XmlSerializer(typeof(TValue));
27 
28             
29             write.WriteAttributeString("name", name);
30             
31             write.WriteStartElement(name);
32             foreach (KeyValuePair<TKey, TValue> kv in this)
33             {
34                 write.WriteStartElement("element");
35                 write.WriteStartElement("key");
36                 KeySerializer.Serialize(write, kv.Key);
37                 write.WriteEndElement();
38                 write.WriteStartElement("value");
39                 ValueSerializer.Serialize(write, kv.Value);
40                 write.WriteEndElement();
41                 write.WriteEndElement();
42             }
43             write.WriteEndElement();
44         }
45         public void ReadXml(XmlReader reader)       // Deserializer
46         {
47             reader.Read();
48             XmlSerializer KeySerializer = new XmlSerializer(typeof(TKey));
49             XmlSerializer ValueSerializer = new XmlSerializer(typeof(TValue));
50         
51             name = reader.Name;
52             reader.ReadStartElement(name);
53             while (reader.NodeType != XmlNodeType.EndElement)
54             {
55                 reader.ReadStartElement("element");
56                 reader.ReadStartElement("key");
57                 TKey tk = (TKey)KeySerializer.Deserialize(reader);
58                 reader.ReadEndElement();
59                 reader.ReadStartElement("value");
60                 TValue vl = (TValue)ValueSerializer.Deserialize(reader);
61                 reader.ReadEndElement();
62                 reader.ReadEndElement();
63                 
64                 this.Add(tk, vl);
65                 reader.MoveToContent();
66             }
67             reader.ReadEndElement();
68             reader.ReadEndElement();
69 
70         }
71         public XmlSchema GetSchema()
72         {
73             return null;
74         }
75     }
76     class Program
77     {
78         static void Main(string[] args)
79         {
80             SerialDictionary<string, uint> sd = new SerialDictionary<string, uint>("modelNextIDs");
81             sd.Add("xxxx", 10);
82             sd.Add("xxxxx", 1);
83             sd.Add("xxxxxx", 1);
84             string fileName = "D:\\s.xml";
85             using (FileStream fileStream = new FileStream(fileName, FileMode.Create))
86             {
87                 XmlSerializer xmlFormatter = new XmlSerializer(sd.GetType());
88                 xmlFormatter.Serialize(fileStream, sd);
89             }
90 
91             SerialDictionary<string, uint> sd2 = new SerialDictionary<string, uint>("modelNextIDs");
92             using (FileStream fileStream2 = new FileStream(fileName, FileMode.Open))
93             {
94                 XmlSerializer xmlFormatter = new XmlSerializer(sd2.GetType());
95                 sd2 = xmlFormatter.Deserialize(fileStream2) as SerialDictionary<string, uint>;
96             }
97         }
98     }
99 }

 

posted @ 2014-01-07 22:54  Gomen  阅读(2493)  评论(0编辑  收藏  举报