コード例 #1
0
        /// <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
            XmlSerializer keySerializer   = new XmlSerializer(m_tKey);
            XmlSerializer valueSerializer = new XmlSerializer(m_tValue);

            // Get key-value pair name
            string keyValuePairName = SerializableGenerics.GetKeyValuePairName(m_tKey, m_tValue);

            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();
            }
        }
コード例 #2
0
        /// <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
            XmlSerializer keySerializer   = new XmlSerializer(m_tKey);
            XmlSerializer valueSerializer = new XmlSerializer(m_tValue);

            // Get key-value pair name
            string keyValuePairName = SerializableGenerics.GetKeyValuePairName(m_tKey, m_tValue);

            // 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
                TKey   key   = (TKey)keySerializer.Deserialize(reader);
                TValue 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();
        }
コード例 #3
0
 /// <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()));
 }