如何序列化C#Dictionary<int, string>
?
答案 0 :(得分:4)
这是一个简单的演示:
var lookup = new Dictionary<int, string>();
lookup.Add(1, "123");
lookup.Add(2, "456");
using (var ms = new MemoryStream())
{
var formatter =
new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
formatter.Serialize(ms, lookup);
lookup = null;
ms.Position = 0;
lookup = (Dictionary<int, string>) formatter.Deserialize(ms);
}
foreach(var i in lookup.Keys)
{
Console.WriteLine("{0}: {1}", i, lookup[i]);
}
但你可能必须更加具体。
答案 1 :(得分:1)
假设您正在讨论XML序列化,您可以使用Paul Welter的SerializableDictionary
类,如Kevin所建议的那样,但这是另一种解决方案,不涉及IXmlSerializable
的自定义实现。
我的想法是,字典可以看作是键值对的集合。 XmlSerializer
可以序列化集合,并且可以序列化键值对。所以你只需要为字典创建一个包装器,它似乎是一个集合,这样XmlSerializer
就可以在不抱怨的情况下处理它。
这是我的实施:
用于保存键值对的XmlDictionaryEntry
类(KeyValuePair<TKey, TValue>
类无法序列化为XML,因为其属性是只读的)
public class XmlDictionaryEntry<TKey, TValue>
{
public TKey Key { get; set; }
public TValue Value { get; set; }
}
一个XmlDictionaryEntryCollection
类,实现一组键值对并使用字典来存储它们
public class XmlDictionaryEntryCollection<TKey, TValue> : ICollection<XmlDictionaryEntry<TKey, TValue>>
{
public XmlDictionaryEntryCollection()
{
this.Dictionary = new Dictionary<TKey, TValue>();
}
public XmlDictionaryEntryCollection(IDictionary<TKey, TValue> dictionary)
{
dictionary.CheckArgumentNull("dictionary");
this.Dictionary = dictionary;
}
[XmlIgnore]
public IDictionary<TKey, TValue> Dictionary { get; private set; }
#region ICollection<XmlDictionaryEntry<TKey,TValue>> Members
public void Add(XmlDictionaryEntry<TKey, TValue> item)
{
this.Dictionary.Add(item.Key, item.Value);
}
public void Clear()
{
this.Dictionary.Clear();
}
public bool Contains(XmlDictionaryEntry<TKey, TValue> item)
{
return this.Dictionary.ContainsKey(item.Key);
}
public void CopyTo(XmlDictionaryEntry<TKey, TValue>[] array, int arrayIndex)
{
int index = arrayIndex;
if (arrayIndex + this.Dictionary.Count > array.Length)
throw new ArgumentException(ExceptionMessages.CopyToNotEnoughSpace);
foreach (var kvp in this.Dictionary)
{
var entry = new XmlDictionaryEntry<TKey, TValue>
{
Key = kvp.Key,
Value = kvp.Value
};
array[index++] = entry;
}
}
public int Count
{
get { return this.Dictionary.Count; }
}
public bool IsReadOnly
{
get { return this.Dictionary.IsReadOnly; }
}
public bool Remove(XmlDictionaryEntry<TKey, TValue> item)
{
return this.Dictionary.Remove(item.Key);
}
#endregion
#region IEnumerable<XmlDictionaryEntry<TKey,TValue>> Members
public IEnumerator<XmlDictionaryEntry<TKey, TValue>> GetEnumerator()
{
foreach (var kvp in this.Dictionary)
{
yield return new XmlDictionaryEntry<TKey, TValue>
{
Key = kvp.Key,
Value = kvp.Value
};
}
}
#endregion
#region IEnumerable Members
IEnumerator IEnumerable.GetEnumerator()
{
return this.GetEnumerator();
}
#endregion
}
一种扩展方法,可以更容易地创建包装器(利用泛型类型推断):
public static class XmlSerializationExtension()
{
public static XmlDictionaryEntryCollection<TKey, TValue> AsXmlSerializable<TKey, TValue>(this IDictionary<TKey, TValue> dictionary)
{
if (dictionary != null)
return new XmlDictionaryEntryCollection<TKey, TValue>(dictionary);
else
return null;
}
}
以下是您使用它的方式:
假设你有这个属性:
public Dictionary<string, Foo> Foos { get; set; }
您只需要在序列化程序中隐藏该属性(使用XmlIgnore
属性),然后序列化XmlDictionaryEntryCollection
:
[XmlIgnore]
public Dictionary<string, Foo> Foos { get; set; }
[XmlElement("Foos")]
public XmlDictionaryEntryCollection SerializableFoos
{
get { return Foos.AsXmlSerializable(); }
set { Foos = value.Dictionary; }
}
这种技术相对于SerializableDictionary
类的主要好处是它允许你使用你想要的任何类型的字典,你不会被特定的实现所困扰。
答案 2 :(得分:0)
您可以编写自己的对象,该对象具有以下两种方法:Serialize和DeSerialize。 Form_Load用于测试
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
Dictionary<int, string> list = new Dictionary<int, string>();
list.Add(1, "one");
list.Add(2, "two");
list.Add(3, "three");
Dictionary<int, string> list2 = Deserialize(Serialize(list));
}
public string Serialize(Dictionary<int, string> classObject)
{
StringBuilder output = new StringBuilder();
output.Append("<DictionaryIntString>");
foreach (int key in classObject.Keys)
{
output.Append(String.Format("<Key value=\"{0}\">",key));
output.Append(String.Format("<Value>{0}</Value></Key>", classObject[key]));
}
output.Append("</DictionaryIntString>");
return output.ToString();
}
public Dictionary<int, string> Deserialize(string input)
{
Dictionary<int, string> output = new Dictionary<int, string>();
XmlDocument xml = new XmlDocument();
xml.LoadXml(input);
foreach (XmlNode node in xml.GetElementsByTagName("Key"))
{
output.Add(Int32.Parse(node.Attributes["value"].InnerText),node.FirstChild.InnerText);
}
return output;
}
}