/// <summary> /// Converts an object into its XML representation. /// </summary> /// <param name="writer">The System.Xml.XmlWriter stream to which the object is serialized.</param> void IXmlSerializable.WriteXml(XmlWriter writer) { // Create xml serializers for key and value var keySerializer = new XmlSerializer(_mTKey); var valueSerializer = new XmlSerializer(_mTValue); // Get key-value pair name string keyValuePairName = SerializableGenerics.GetKeyValuePairName(_mTKey, _mTValue); IEnumerator <KeyValuePair <TKey, TValue> > enumerator = GetEnumerator(); while (enumerator.MoveNext()) { // Get current key value pair KeyValuePair <TKey, TValue> keyValuePair = enumerator.Current; // Write start element with key-value pair name writer.WriteStartElement(keyValuePairName); // Serialize key and value keySerializer.Serialize(writer, keyValuePair.Key); valueSerializer.Serialize(writer, keyValuePair.Value); // Write end element with key-value pair name writer.WriteEndElement(); } }
/// <summary> /// Generates an object from its XML representation. /// </summary> /// <param name="reader">The System.Xml.XmlReader stream from which the object is deserialized.</param> void IXmlSerializable.ReadXml(XmlReader reader) { // Create xml serializers for key and value var keySerializer = new XmlSerializer(_mTKey); var valueSerializer = new XmlSerializer(_mTValue); // Get key-value pair name string keyValuePairName = SerializableGenerics.GetKeyValuePairName(_mTKey, _mTValue); // Read start element and move to content reader.ReadStartElement(); reader.MoveToContent(); // Is keyValuePairName the start element if (!reader.IsStartElement(keyValuePairName)) { // Throw an exception throw new XmlException("Starting element " + keyValuePairName + " not found."); } // Loop through key-value pairs while (reader.IsStartElement(keyValuePairName)) { // Read key-value pair and move to content reader.ReadStartElement(keyValuePairName); reader.MoveToContent(); // Deserialize key and value var key = (TKey)keySerializer.Deserialize(reader); var value = (TValue)valueSerializer.Deserialize(reader); // Read end element and add key-value pair to dictionary reader.ReadEndElement(); Add(key, value); } // Read end element and move to content reader.ReadEndElement(); reader.MoveToContent(); }
/// <summary> /// Returns a string that represents the current SerializableDictionary. /// </summary> /// <returns> /// A string that represents the current SerializableDictionary. /// </returns> public override string ToString() { return(SerializableGenerics.GetTypeName(GetType())); }