代码之家  ›  专栏  ›  技术社区  ›  Luca

控制字典的XML序列化<k,t>

  •  0
  • Luca  · 技术社区  · 14 年前

    我正在研究XML序列化,因为我使用了很多字典,所以我也希望对它们进行序列化。我找到了下面的解决方案(我很自豪!:)

    [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>();
    }
    

    从该类派生的类按以下方式序列化:

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

    这给了我一个很好的解决方案,但是:

    • 如何控制元素的名称 字典项目信息32foo
    • 如果我定义了 Dictionary<FooInt32, Int32> 我有课 Foo FooInt32 ?
    • 是否可以优化上面的类?

    非常感谢你!

    2 回复  |  直到 14 年前
        1
  •  0
  •   WayneC    14 年前

    使用 DataContractSerializer 用于序列化并使用 CollectionDataContractAttribute 控制输出。

    [CollectionDataContract(Name="MyDictionary", ItemName="MyDictionaryItem")]
    public class XmlDictionary<TKey, TValue>
    {
        ...
    }
    
        2
  •  0
  •   Jouke van der Maas    14 年前

    您可以在类上设置[xmlElement(elementname=“name”)]。不过我还没试过,你可能得把它放在别的地方。无论如何,在某个地方设置elementname是一种方法。