控制 Dictionary<K, T> 的 XML 序列化 [英] Control XML serialization of Dictionary<K, T>
本文介绍了控制 Dictionary<K, T> 的 XML 序列化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在研究 XML 序列化,因为我使用了很多字典,所以我也想对它们进行序列化.我为此找到了以下解决方案(我为此感到非常自豪!:)).
I'm investigating about XML serialization, and since I use lot of dictionary, I would like to serialize them as well. I found the following solution for that (I'm quite 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>();
}
从该类派生的类按以下方式序列化:
The classes deriving from this class are serialized in the following way:
<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 give me a good solution, but:
- 如何控制元素的名称DictionaryItemOfInt32Foo
- 如果我定义了一个
Dictionary
并且我有类Foo
和FooInt32
会发生什么? - 是否可以优化上面的类?
- How can I control the name of the element DictionaryItemOfInt32Foo
- What happens if I define a
Dictionary<FooInt32, Int32>
and I have the classesFoo
andFooInt32
? - Is it possible to optimize the class above?
非常感谢!
推荐答案
使用 DataContractSerializer 用于序列化并使用 CollectionDataContractAttribute 控制输出.
Use DataContractSerializer for serialization and use the CollectionDataContractAttribute to control the output.
[CollectionDataContract(Name="MyDictionary", ItemName="MyDictionaryItem")]
public class XmlDictionary<TKey, TValue>
{
...
}
这篇关于控制 Dictionary<K, T> 的 XML 序列化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文