Code/C#
Generic Dictionary Xml Serialization C# Example 1
Hide Code
2007. 12. 16. 07:18
Generic Dictionary는 Xml.Serialization.XmlSerializer을 사용해서 serialize할 수 없다.
그래서 Generic Dictionary을 XML 파일로 serialize할 수 있는 GenericDictionaryXmlSerializer 클래스를 만들어 보았다.
이 클래스는 아래 링크에 있는 코드를 응용해서 만든 것이다.
http://blogs.msdn.com/psheill/archive/2005/04/09/406823.aspx
위의 링크에 있는 코드는 Generic 클래스를 사용하지 않았는데, 필자의 코드는 Generic 코드를 사용한 점이 다르다.
using System;
using System.Collections.Generic;
using System.Xml;
using System.Xml.Serialization;
namespace GenericDictionaryXmlSerialization
{
public class GenericDictionaryXmlSerializer<TKey, TValue>
{
public void Serialize(XmlWriter writer, Dictionary<TKey, TValue> dictionary)
{
List<Entry<TKey, TValue>> entries = new List<Entry<TKey, TValue>>(dictionary.Count);
foreach (KeyValuePair<TKey, TValue> item in dictionary)
{
entries.Add(new Entry<TKey, TValue>(item.Key, item.Value));
}
XmlSerializer serializer = new XmlSerializer(typeof(List<Entry<TKey, TValue>>));
serializer.Serialize(writer, entries);
}
public void Deserialize(XmlReader reader, Dictionary<TKey, TValue> dictionary)
{
XmlSerializer serializer = new XmlSerializer(typeof(List<Entry<TKey, TValue>>));
object o = serializer.Deserialize(reader);
List<Entry<TKey, TValue>> entries = new List<Entry<TKey, TValue>>();
entries = o as List<Entry<TKey, TValue>>;
dictionary.Clear();
foreach (Entry<TKey, TValue> item in entries)
{
dictionary[item.Key] = item.Value;
}
}
}
public class Entry<TKey, TValue>
{
public TKey Key;
public TValue Value;
public Entry()
{
}
public Entry(TKey key, TValue value)
{
Key = key;
Value = value;
}
}
class Program
{
static void Main(string[] args)
{
Dictionary<string, int> myDictionary = new Dictionary<string, int>();
myDictionary["one"] = 1;
myDictionary["two"] = 2;
GenericDictionaryXmlSerializer<string, int> mySerializer = new GenericDictionaryXmlSerializer<string, int>();
using (XmlWriter writer = XmlWriter.Create("serialization.xml"))
{
mySerializer.Serialize(writer, myDictionary);
}
Dictionary<string, int> yourDictionary = new Dictionary<string, int>();
using (XmlReader reader = XmlReader.Create("serialization.xml"))
{
mySerializer.Deserialize(reader, yourDictionary);
}
foreach (KeyValuePair<string, int> item in yourDictionary)
{
Console.WriteLine(item.Key + " : " + item.Value.ToString());
}
}
}
}
그래서 Generic Dictionary을 XML 파일로 serialize할 수 있는 GenericDictionaryXmlSerializer 클래스를 만들어 보았다.
이 클래스는 아래 링크에 있는 코드를 응용해서 만든 것이다.
http://blogs.msdn.com/psheill/archive/2005/04/09/406823.aspx
위의 링크에 있는 코드는 Generic 클래스를 사용하지 않았는데, 필자의 코드는 Generic 코드를 사용한 점이 다르다.
using System;
using System.Collections.Generic;
using System.Xml;
using System.Xml.Serialization;
namespace GenericDictionaryXmlSerialization
{
public class GenericDictionaryXmlSerializer<TKey, TValue>
{
public void Serialize(XmlWriter writer, Dictionary<TKey, TValue> dictionary)
{
List<Entry<TKey, TValue>> entries = new List<Entry<TKey, TValue>>(dictionary.Count);
foreach (KeyValuePair<TKey, TValue> item in dictionary)
{
entries.Add(new Entry<TKey, TValue>(item.Key, item.Value));
}
XmlSerializer serializer = new XmlSerializer(typeof(List<Entry<TKey, TValue>>));
serializer.Serialize(writer, entries);
}
public void Deserialize(XmlReader reader, Dictionary<TKey, TValue> dictionary)
{
XmlSerializer serializer = new XmlSerializer(typeof(List<Entry<TKey, TValue>>));
object o = serializer.Deserialize(reader);
List<Entry<TKey, TValue>> entries = new List<Entry<TKey, TValue>>();
entries = o as List<Entry<TKey, TValue>>;
dictionary.Clear();
foreach (Entry<TKey, TValue> item in entries)
{
dictionary[item.Key] = item.Value;
}
}
}
public class Entry<TKey, TValue>
{
public TKey Key;
public TValue Value;
public Entry()
{
}
public Entry(TKey key, TValue value)
{
Key = key;
Value = value;
}
}
class Program
{
static void Main(string[] args)
{
Dictionary<string, int> myDictionary = new Dictionary<string, int>();
myDictionary["one"] = 1;
myDictionary["two"] = 2;
GenericDictionaryXmlSerializer<string, int> mySerializer = new GenericDictionaryXmlSerializer<string, int>();
using (XmlWriter writer = XmlWriter.Create("serialization.xml"))
{
mySerializer.Serialize(writer, myDictionary);
}
Dictionary<string, int> yourDictionary = new Dictionary<string, int>();
using (XmlReader reader = XmlReader.Create("serialization.xml"))
{
mySerializer.Deserialize(reader, yourDictionary);
}
foreach (KeyValuePair<string, int> item in yourDictionary)
{
Console.WriteLine(item.Key + " : " + item.Value.ToString());
}
}
}
}