Controlling XML Serialization of Dictionary <K, T>

I am learning about XML serialization, and since I use a lot of vocabulary I would like to serialize them as well. I found the following solution for myself (I'm very proud of it :) :).

[XmlInclude(typeof(Foo))]
public class XmlDictionary<TKey, TValue>
{
    /// <summary>
    /// Key/value pair.
    /// </summary>
    public struct DictionaryItem
    {
        /// <summary>
        /// Dictionary item key.
        /// </summary>
        public TKey Key;

        /// <summary>
        /// Dictionary item value.
        /// </summary>
        public TValue Value;
    }

    /// <summary>
    /// Dictionary items.
    /// </summary>
    public DictionaryItem[] Items
    {
        get {
            List<DictionaryItem> items = new List<DictionaryItem>(ItemsDictionary.Count);

            foreach (KeyValuePair<TKey, TValue> pair in ItemsDictionary) {
                DictionaryItem item;

                item.Key = pair.Key;
                item.Value = pair.Value;

                items.Add(item);
            }

            return (items.ToArray());
        }
        set {
            ItemsDictionary = new Dictionary<TKey,TValue>();

            foreach (DictionaryItem item in value)
                ItemsDictionary.Add(item.Key, item.Value);
        }
    }

    /// <summary>
    /// Indexer base on dictionary key.
    /// </summary>
    /// <param name="key"></param>
    /// <returns></returns>
    public TValue this[TKey key]
    {
        get {
            return (ItemsDictionary[key]);
        }
        set {
            Debug.Assert(value != null);
            ItemsDictionary[key] = value;
        }
    }

    /// <summary>
    /// Delegate for get key from a dictionary value.
    /// </summary>
    /// <param name="value"></param>
    /// <returns></returns>
    public delegate TKey GetItemKeyDelegate(TValue value);

    /// <summary>
    /// Add a range of values automatically determining the associated keys.
    /// </summary>
    /// <param name="values"></param>
    /// <param name="keygen"></param>
    public void AddRange(IEnumerable<TValue> values, GetItemKeyDelegate keygen)
    {
        foreach (TValue v in values)
            ItemsDictionary.Add(keygen(v), v);
    }

    /// <summary>
    /// Items dictionary.
    /// </summary>
    [XmlIgnore]
    public Dictionary<TKey, TValue> ItemsDictionary = new Dictionary<TKey,TValue>();
}

      

Classes derived from this class are serialized as follows:

<FooDictionary xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Items>
  <DictionaryItemOfInt32Foo>
    <Key/>
    <Value/>
  </DictionaryItemOfInt32XmlProcess>
<Items>

      

This gives me a good solution, but:

  • How can I manage the item name DictionaryItemOfInt32Foo
  • What happens if I define a Dictionary<FooInt32, Int32>

    and I have classes Foo

    and FooInt32

    ?
  • Can the class above be optimized?

READ! Very important!

0


a source to share


2 answers


Use DataContractSerializer for serialization and use CollectionDataContractAttribute to control output.



[CollectionDataContract(Name="MyDictionary", ItemName="MyDictionaryItem")]
public class XmlDictionary<TKey, TValue>
{
    ...
}

      

0


a source


You can set [XmlElement (ElementName = "name")] in the class. I haven't tried this, although you may need to install it somewhere else. Either way, setting ElementName is the way to go.



0


a source







All Articles