/////////////////////////////////////////////////////////////////////////// // Copyright (C) Wizardry and Steamworks 2013 - License: GNU GPLv3 // // Please see: http://www.gnu.org/licenses/gpl.html for legal details, // // rights of fair usage, the disclaimer and warranty conditions. // /////////////////////////////////////////////////////////////////////////// using System; using System.Collections.Generic; using System.IO; using System.Xml; using System.Xml.Schema; using System.Xml.Serialization; namespace wasSharp.Collections.Generic { /// /// A serializable sorted dictionary class. /// /// the key /// the value [XmlRoot("SortedDictionary")] public class SerializableSortedDictionary : SortedDictionary, IXmlSerializable { #region IXmlSerializable Members public SerializableSortedDictionary(IEnumerable> kvp) { foreach (var i in kvp) { Add(i.Key, i.Value); } } public SerializableSortedDictionary() { } /// /// Deep-clones the serializable dictionary. /// /// a deep clone of the original dictionary public SerializableSortedDictionary Clone() { SerializableSortedDictionary clone; try { using (var writer = new MemoryStream()) { var serializer = new XmlSerializer( typeof(SerializableDictionary)); serializer.Serialize(writer, this); writer.Position = 0; clone = (SerializableSortedDictionary) new XmlSerializer( typeof(SerializableSortedDictionary)) .Deserialize(writer); } } /* cloning failed so return an empty dictionary */ catch (Exception) { clone = new SerializableSortedDictionary(); } return clone; } public XmlSchema GetSchema() { return null; } public void ReadXml(XmlReader reader) { var keySerializer = new XmlSerializer(typeof(TKey)); var valueSerializer = new XmlSerializer(typeof(TValue)); var wasEmpty = reader.IsEmptyElement; reader.Read(); if (wasEmpty) return; while (!reader.NodeType.Equals(XmlNodeType.EndElement)) { reader.ReadStartElement("Item"); reader.ReadStartElement("Key"); var key = (TKey)keySerializer.Deserialize(reader); reader.ReadEndElement(); reader.ReadStartElement("Value"); var value = (TValue)valueSerializer.Deserialize(reader); reader.ReadEndElement(); Add(key, value); reader.ReadEndElement(); reader.MoveToContent(); } reader.ReadEndElement(); } public void WriteXml(XmlWriter writer) { var keySerializer = new XmlSerializer(typeof(TKey)); var valueSerializer = new XmlSerializer(typeof(TValue)); foreach (var key in Keys) { writer.WriteStartElement("Item"); writer.WriteStartElement("Key"); keySerializer.Serialize(writer, key); writer.WriteEndElement(); writer.WriteStartElement("Value"); var value = this[key]; valueSerializer.Serialize(writer, value); writer.WriteEndElement(); writer.WriteEndElement(); } } #endregion IXmlSerializable Members } }