|
using System;
using System.Collections.Generic;
using System.Text;
using System.Xml.Serialization;
namespace Mullivan.Collections.Generic
{
/// <summary>
/// A serializable dictionary class.
/// </summary>
/// <typeparam name="TKey">The dictionary key object.</typeparam>
/// <typeparam name="TValue">The dictionary value object.</typeparam>
[XmlRoot("dictionary")]
public class SerializableDictionary<TKey, TValue>
: Dictionary<TKey, TValue>, IXmlSerializable
{
/// <summary>
/// Initializes a new instance of the <c>SerializableDictionary</c> class.
/// </summary>
public SerializableDictionary()
: base()
{ }
/// <summary>
/// Gets or sets a value from the <c>SerializableDictionary</c> class based on a key.
/// </summary>
/// <param name="key">The key object used to retrieve the value from the dictionary.</param>
/// <returns>A value object based on the given key.</returns>
public new TValue this[TKey key]
{
get
{
if (ContainsKey(key))
return base[key];
else
return default(TValue);
}
set
{
if (ContainsKey(key))
base[key] = value;
else
this.Add(key, value);
}
}
#region IXmlSerializable Members
/// <summary>
/// Gets the <c>XmlSchema</c> of the class.
/// </summary>
/// <returns>The <c>XmlSchema</c> of the class.</returns>
public System.Xml.Schema.XmlSchema GetSchema()
{
return null;
}
/// <summary>
/// Deserializes a <c>SerializableDictionary</c> object from a given <c>XmlReader</c> object
/// and stores the data in the current instantiated <c>SerializableDictionary</c> object.
/// </summary>
/// <param name="reader">
/// The <c>XmlReader</c> object that provides the xml information to be deserialized.
/// </param>
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;
reader.MoveToContent();
while (reader.Name == "item" && reader.NodeType == System.Xml.XmlNodeType.Element)
{
reader.ReadStartElement("item");
reader.ReadStartElement("key");
TKey key = (TKey)keySerializer.Deserialize(reader);
reader.ReadEndElement();
reader.ReadStartElement("value");
TValue value = (TValue)valueSerializer.Deserialize(reader);
this.Add(key, value);
reader.ReadEndElement();
reader.ReadEndElement();
reader.MoveToContent();
}
reader.ReadEndElement();
}
/// <summary>
/// Serializes the current <c>SerializableDictionary</c> object into an xml file
/// using the given <c>XmlWriter</c>.
/// </summary>
/// <param name="writer">
/// The <c>XmlWriter</c> object used for serialization of the
/// <c>SerializableDictionary</c> object.
/// </param>
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();
}
}
#endregion
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.