using System.Collections.Generic; using System.Xml.Serialization; /// /// For some reason, the generic Dictionary in .net 2.0 is not XML serializable. The following code snippet is a xml serializable generic dictionary. The dictionary is serializable by implementing the IXmlSerializable interface. /// Also see https://weblogs.asp.net/pwelter34/444961 for additional information. /// /// Type of the dictionary key. /// Type of the dictionary value. [XmlRoot("dictionary")] public class SerializableDictionary : Dictionary, IXmlSerializable { /// /// Initializes a new instance of the class. /// public SerializableDictionary() { // Empty } /// /// Initializes a new instance of the class. /// /// Dictionary to convert from. public SerializableDictionary(IDictionary dictionary) : base(dictionary) { // Empty } /// /// Initializes a new instance of the class. /// /// Dictionary to convert from. /// Comparer for the dictionary. public SerializableDictionary(IDictionary dictionary, IEqualityComparer comparer) : base(dictionary, comparer) { // Empty } /// /// Initializes a new instance of the class. /// /// Comparer for the dictionary. public SerializableDictionary(IEqualityComparer comparer) : base(comparer) { // Empty } /// /// Initializes a new instance of the class. /// /// Capacity of the dictionary. public SerializableDictionary(int capacity) : base(capacity) { // Empty } /// /// Initializes a new instance of the class. /// /// Capacity of the dictionary. /// Comparer for the dictionary. public SerializableDictionary(int capacity, IEqualityComparer comparer) : base(capacity, comparer) { // Empty } /// /// Gets the schema of the XML object. /// /// Nothing. public System.Xml.Schema.XmlSchema GetSchema() { return null; } /// /// Reads XML and changes this object to be an instance of that data. /// /// The XML reader to read from. public void ReadXml(System.Xml.XmlReader reader) { XmlSerializer keySerializer = new XmlSerializer(typeof(TKey)); XmlSerializer valueSerializer = new XmlSerializer(typeof(TValue)); bool wasEmpty = reader.IsEmptyElement; reader.Read(); if (wasEmpty) { return; } while (reader.NodeType != System.Xml.XmlNodeType.EndElement) { reader.ReadStartElement("item"); reader.ReadStartElement("key"); TKey key = (TKey)keySerializer.Deserialize(reader); reader.ReadEndElement(); reader.ReadStartElement("value"); TValue value = (TValue)valueSerializer.Deserialize(reader); reader.ReadEndElement(); this.Add(key, value); reader.ReadEndElement(); reader.MoveToContent(); } reader.ReadEndElement(); } /// /// Writes XML to the XML writer from this object. /// /// An instance of the XmlWriter class. public void WriteXml(System.Xml.XmlWriter writer) { XmlSerializer keySerializer = new XmlSerializer(typeof(TKey)); XmlSerializer valueSerializer = new XmlSerializer(typeof(TValue)); foreach (TKey key in this.Keys) { writer.WriteStartElement("item"); writer.WriteStartElement("key"); keySerializer.Serialize(writer, key); writer.WriteEndElement(); writer.WriteStartElement("value"); TValue value = this[key]; valueSerializer.Serialize(writer, value); writer.WriteEndElement(); writer.WriteEndElement(); } } }