Advertisement
BurningBunny

C# XMLSerializableDictionary class

Jun 25th, 2013
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.57 KB | None | 0 0
  1. /*
  2.  *  For more juicy code snippets, visit http://csharpcodewhisperer.blogspot.com
  3.  */
  4. namespace XMLSerializableDictionary
  5. {
  6.     using System;
  7.     using System.Collections.Generic;
  8.     using System.Runtime.Serialization;
  9.     using System.Xml;
  10.     using System.Xml.Schema;
  11.     using System.Xml.Serialization;
  12.  
  13.  
  14.     /// <summary>
  15.     /// Represents an XML serializable collection of keys and values.
  16.     /// </summary>
  17.     /// <typeparam name="TKey">The type of the keys in the dictionary.</typeparam>
  18.     /// <typeparam name="TValue">The type of the values in the dictionary.</typeparam>
  19.     [Serializable]
  20.     [XmlRoot("Dictionary")]
  21.     public class SerializableDictionary<TKey, TValue> : Dictionary<TKey, TValue>, IXmlSerializable
  22.     {
  23.         /// <summary>
  24.         /// The default XML tag name for an item.
  25.         /// </summary>
  26.         private const string DefaultTag_Item = "Item";
  27.  
  28.         /// <summary>
  29.         /// The default XML tag name for a key.
  30.         /// </summary>
  31.         private const string DefaultTag_Key = "Key";
  32.  
  33.         /// <summary>
  34.         /// The default XML tag name for a value.
  35.         /// </summary>
  36.         private const string DefaultTag_Value = "Value";
  37.  
  38.         /// <summary>
  39.         /// The XML serializer for the key type.
  40.         /// </summary>
  41.         private static readonly XmlSerializer keySerializer = new XmlSerializer(typeof(TKey));
  42.  
  43.         /// <summary>
  44.         /// The XML serializer for the value type.
  45.         /// </summary>
  46.         private static readonly XmlSerializer valueSerializer = new XmlSerializer(typeof(TValue));
  47.  
  48.         /// <summary>
  49.         /// Initializes a new instance of the
  50.         /// <see cref="SerializableDictionary&lt;TKey, TValue&gt;"/> class.
  51.         /// </summary>
  52.         public SerializableDictionary() : base()
  53.         {
  54.         }
  55.  
  56.         /// <summary>
  57.         /// Initializes a new instance of the
  58.         /// <see cref="SerializableDictionary&lt;TKey, TValue&gt;"/> class.
  59.         /// </summary>
  60.         /// <param name="info">A
  61.         ///   <see cref="T:System.Runtime.Serialization.SerializationInfo"/> object
  62.         ///   containing the information required to serialize the
  63.         ///   <see cref="T:System.Collections.Generic.Dictionary`2"/>.
  64.         /// </param>
  65.         /// <param name="context">A
  66.         ///   <see cref="T:System.Runtime.Serialization.StreamingContext"/> structure
  67.         ///   containing the source and destination of the serialized stream
  68.         ///   associated with the
  69.         ///   <see cref="T:System.Collections.Generic.Dictionary`2"/>.
  70.         /// </param>
  71.         protected SerializableDictionary(SerializationInfo info,StreamingContext context) : base(info, context)
  72.         {
  73.         }
  74.  
  75.         /// <summary>
  76.         /// Gets the XML tag name for an item.
  77.         /// </summary>
  78.         protected virtual string ItemTagName
  79.         {
  80.             get { return DefaultTag_Item; }
  81.         }
  82.  
  83.         /// <summary>
  84.         /// Gets the XML tag name for a key.
  85.         /// </summary>
  86.         protected virtual string KeyTagName
  87.         {
  88.             get { return DefaultTag_Key; }
  89.         }
  90.  
  91.         /// <summary>
  92.         /// Gets the XML tag name for a value.
  93.         /// </summary>
  94.         protected virtual string ValueTagName
  95.         {
  96.             get { return DefaultTag_Value; }
  97.         }
  98.  
  99.         /// <summary>
  100.         /// Gets the XML schema for the XML serialization.
  101.         /// </summary>
  102.         /// <returns>An XML schema for the serialized object.</returns>
  103.         public XmlSchema GetSchema()
  104.         {
  105.             return null;
  106.         }
  107.  
  108.         /// <summary>
  109.         /// Deserializes the object from XML.
  110.         /// </summary>
  111.         /// <param name="reader">The XML representation of the object.</param>
  112.         public void ReadXml(XmlReader reader)
  113.         {
  114.             bool wasEmpty = reader.IsEmptyElement;
  115.  
  116.             reader.Read();
  117.  
  118.             if(wasEmpty) { return; }
  119.  
  120.             try
  121.             {
  122.                 while (reader.NodeType != XmlNodeType.EndElement)
  123.                 {
  124.                     reader.ReadStartElement(this.ItemTagName);
  125.                     try
  126.                     {
  127.                         TKey Key;
  128.                         TValue Value;
  129.  
  130.                         reader.ReadStartElement(this.KeyTagName);
  131.                         try
  132.                         {
  133.                             Key = (TKey)keySerializer.Deserialize(reader);
  134.                         }
  135.                         finally
  136.                         {
  137.                             reader.ReadEndElement();
  138.                         }
  139.  
  140.                         reader.ReadStartElement(this.ValueTagName);
  141.                         try
  142.                         {
  143.                             Value = (TValue)valueSerializer.Deserialize(reader);
  144.                         }
  145.                         finally
  146.                         {
  147.                             reader.ReadEndElement();
  148.                         }
  149.  
  150.                         this.Add(Key, Value);
  151.                     }
  152.                     finally
  153.                     {
  154.                         reader.ReadEndElement();
  155.                     }
  156.  
  157.                     reader.MoveToContent();
  158.                 }
  159.             }
  160.             finally
  161.             {
  162.                 reader.ReadEndElement();
  163.             }
  164.         }
  165.  
  166.         /// <summary>
  167.         /// Serializes this instance to XML.
  168.         /// </summary>
  169.         /// <param name="writer">The writer to serialize to.</param>
  170.         public void WriteXml(XmlWriter writer)
  171.         {
  172.             foreach (KeyValuePair<TKey,TValue> keyValuePair in this)
  173.             {
  174.                 writer.WriteStartElement(this.ItemTagName);
  175.                 try
  176.                 {
  177.                     writer.WriteStartElement(this.KeyTagName);
  178.                     try
  179.                     {
  180.                         keySerializer.Serialize(writer, keyValuePair.Key);
  181.                     }
  182.                     finally
  183.                     {
  184.                         writer.WriteEndElement();
  185.                     }
  186.  
  187.                     writer.WriteStartElement(this.ValueTagName);
  188.                     try
  189.                     {
  190.                         valueSerializer.Serialize(writer, keyValuePair.Value);
  191.                     }
  192.                     finally
  193.                     {
  194.                         writer.WriteEndElement();
  195.                     }
  196.                 }
  197.                 finally
  198.                 {
  199.                     writer.WriteEndElement();
  200.                 }
  201.             }
  202.         }
  203.     }
  204. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement