1 using System; 2 using System.Runtime.Serialization; 3 using System.Xml; 4 using System.Xml.Serialization; 5 using System.Collections.Generic; 6 using System.Text; 7 8 namespace Sample 9 { 10 [Serializable()] 11 public class SerializableDictionary<TKey, TVal> : Dictionary<TKey, TVal>, IXmlSerializable, ISerializable 12 { 13 #region Constants 14 private const string DictionaryNodeName = " Dictionary "; 15 private const string ItemNodeName = " Item "; 16 private const string KeyNodeName = " Key "; 17 private const string ValueNodeName = " Value "; 18 #endregion 19 #region Constructors 20 public SerializableDictionary() 21 { 22 } 23 24 public SerializableDictionary(IDictionary<TKey, TVal> dictionary) 25 : base(dictionary) 26 { 27 } 28 29 public SerializableDictionary(IEqualityComparer<TKey> comparer) 30 : base(comparer) 31 { 32 } 33 34 public SerializableDictionary( int capacity) 35 : base(capacity) 36 { 37 } 38 39 public SerializableDictionary(IDictionary<TKey, TVal> dictionary, IEqualityComparer<TKey> comparer) 40 : base(dictionary, comparer) 41 { 42 } 43 44 public SerializableDictionary( int capacity, IEqualityComparer<TKey> comparer) 45 : base(capacity, comparer) 46 { 47 } 48 49 #endregion 50 #region ISerializable Members 51 52 protected SerializableDictionary(SerializationInfo info, StreamingContext context) 53 { 54 int itemCount = info.GetInt32( " ItemCount "); 55 for ( int i = 0; i < itemCount; i++) 56 { 57 KeyValuePair<TKey, TVal> kvp = (KeyValuePair<TKey, TVal>)info.GetValue(String.Format( " Item{0} ", i), typeof(KeyValuePair<TKey, TVal>)); 58 this.Add(kvp.Key, kvp.Value); 59 } 60 } 61 62 void ISerializable.GetObjectData(SerializationInfo info, StreamingContext context) 63 { 64 info.AddValue( " ItemCount ", this.Count); 65 int itemIdx = 0; 66 foreach (KeyValuePair<TKey, TVal> kvp in this) 67 { 68 info.AddValue(String.Format( " Item{0} ", itemIdx), kvp, typeof(KeyValuePair<TKey, TVal>)); 69 itemIdx++; 70 } 71 } 72 73 #endregion 74 #region IXmlSerializable Members 75 76 void IXmlSerializable.WriteXml(System.Xml.XmlWriter writer) 77 { 78 // writer.WriteStartElement(DictionaryNodeName); 79 foreach (KeyValuePair<TKey, TVal> kvp in this) 80 { 81 writer.WriteStartElement(ItemNodeName); 82 writer.WriteStartElement(KeyNodeName); 83 KeySerializer.Serialize(writer, kvp.Key); 84 writer.WriteEndElement(); 85 writer.WriteStartElement(ValueNodeName); 86 ValueSerializer.Serialize(writer, kvp.Value); 87 writer.WriteEndElement(); 88 writer.WriteEndElement(); 89 } 90 // writer.WriteEndElement(); 91 } 92 93 void IXmlSerializable.ReadXml(System.Xml.XmlReader reader) 94 { 95 if (reader.IsEmptyElement) 96 { 97 return; 98 } 99 100 // Move past container 101 if (!reader.Read()) 102 { 103 throw new XmlException( " Error in Deserialization of Dictionary "); 104 } 105 106 // reader.ReadStartElement(DictionaryNodeName); 107 while (reader.NodeType != XmlNodeType.EndElement) 108 { 109 reader.ReadStartElement(ItemNodeName); 110 reader.ReadStartElement(KeyNodeName); 111 TKey key = (TKey)KeySerializer.Deserialize(reader); 112 reader.ReadEndElement(); 113 reader.ReadStartElement(ValueNodeName); 114 TVal value = (TVal)ValueSerializer.Deserialize(reader); 115 reader.ReadEndElement(); 116 reader.ReadEndElement(); 117 this.Add(key, value); 118 reader.MoveToContent(); 119 } 120 // reader.ReadEndElement(); 121 122 reader.ReadEndElement(); // Read End Element to close Read of containing node 123 } 124 125 System.Xml.Schema.XmlSchema IXmlSerializable.GetSchema() 126 { 127 return null; 128 } 129 130 #endregion 131 #region Private Properties 132 protected XmlSerializer ValueSerializer 133 { 134 get 135 { 136 if (valueSerializer == null) 137 { 138 valueSerializer = new XmlSerializer( typeof(TVal)); 139 } 140 return valueSerializer; 141 } 142 } 143 144 private XmlSerializer KeySerializer 145 { 146 get 147 { 148 if (keySerializer == null) 149 { 150 keySerializer = new XmlSerializer( typeof(TKey)); 151 } 152 return keySerializer; 153 } 154 } 155 #endregion 156 #region Private Members 157 private XmlSerializer keySerializer = null; 158 private XmlSerializer valueSerializer = null; 159 #endregion 160 } 161 }