wasSharp – Blame information for rev 27

Subversion Repositories:
Rev:
Rev Author Line No. Line
10 office 1 ///////////////////////////////////////////////////////////////////////////
2 // Copyright (C) Wizardry and Steamworks 2013 - License: GNU GPLv3 //
3 // Please see: http://www.gnu.org/licenses/gpl.html for legal details, //
4 // rights of fair usage, the disclaimer and warranty conditions. //
5 ///////////////////////////////////////////////////////////////////////////
6  
7 using System;
8 using System.Collections.Generic;
9 using System.IO;
10 using System.Xml;
11 using System.Xml.Schema;
12 using System.Xml.Serialization;
13  
14 namespace wasSharp.Collections.Generic
15 {
16 /// <summary>
17 /// A serializable sorted dictionary class.
18 /// </summary>
19 /// <typeparam name="TKey">the key</typeparam>
20 /// <typeparam name="TValue">the value</typeparam>
21 [XmlRoot("SortedDictionary")]
22 public class SerializableSortedDictionary<TKey, TValue>
23 : SortedDictionary<TKey, TValue>, IXmlSerializable
24 {
25 #region IXmlSerializable Members
26  
27 public SerializableSortedDictionary(IEnumerable<KeyValuePair<TKey, TValue>> kvp)
28 {
29 foreach (var i in kvp)
30 {
31 Add(i.Key, i.Value);
32 }
33 }
34  
35 public SerializableSortedDictionary()
36 {
37 }
38  
39 /// <summary>
40 /// Deep-clones the serializable dictionary.
41 /// </summary>
42 /// <returns>a deep clone of the original dictionary</returns>
43 public SerializableSortedDictionary<TKey, TValue> Clone()
44 {
45 SerializableSortedDictionary<TKey, TValue> clone;
46 try
47 {
48 using (var writer = new MemoryStream())
49 {
50 var serializer =
51 new XmlSerializer(
52 typeof(SerializableDictionary<TKey, TValue>));
53 serializer.Serialize(writer, this);
25 office 54 writer.Position = 0;
10 office 55 clone = (SerializableSortedDictionary<TKey, TValue>)
56 new XmlSerializer(
57 typeof(SerializableSortedDictionary<TKey, TValue>))
58 .Deserialize(writer);
59 }
60 }
27 office 61 /* cloning failed so return an empty dictionary */
10 office 62 catch (Exception)
63 {
64 clone = new SerializableSortedDictionary<TKey, TValue>();
65 }
66 return clone;
67 }
68  
69 public XmlSchema GetSchema()
70 {
71 return null;
72 }
73  
74 public void ReadXml(XmlReader reader)
75 {
76 var keySerializer = new XmlSerializer(typeof(TKey));
77 var valueSerializer = new XmlSerializer(typeof(TValue));
78  
79 var wasEmpty = reader.IsEmptyElement;
80 reader.Read();
81  
82 if (wasEmpty)
83 return;
84  
85 while (!reader.NodeType.Equals(XmlNodeType.EndElement))
86 {
87 reader.ReadStartElement("Item");
88  
89 reader.ReadStartElement("Key");
27 office 90 var key = (TKey)keySerializer.Deserialize(reader);
10 office 91 reader.ReadEndElement();
92  
93 reader.ReadStartElement("Value");
27 office 94 var value = (TValue)valueSerializer.Deserialize(reader);
10 office 95 reader.ReadEndElement();
96  
97 Add(key, value);
98  
99 reader.ReadEndElement();
100 reader.MoveToContent();
101 }
102 reader.ReadEndElement();
103 }
104  
105 public void WriteXml(XmlWriter writer)
106 {
107 var keySerializer = new XmlSerializer(typeof(TKey));
108 var valueSerializer = new XmlSerializer(typeof(TValue));
109  
110 foreach (var key in Keys)
111 {
112 writer.WriteStartElement("Item");
113  
114 writer.WriteStartElement("Key");
115 keySerializer.Serialize(writer, key);
116 writer.WriteEndElement();
117  
118 writer.WriteStartElement("Value");
119 var value = this[key];
120 valueSerializer.Serialize(writer, value);
121 writer.WriteEndElement();
122  
123 writer.WriteEndElement();
124 }
125 }
126  
27 office 127 #endregion IXmlSerializable Members
10 office 128 }
27 office 129 }