티스토리 뷰

GenericDictionaryXmlSerialization.GenericDictionaryXmlSerializer 클래스 코드입니다.

앞의 예제에서는 Deserialize mthod를 Deserialize(XmlReader reader, Dictionary<TKey, TValue> dictionary) 형태로 해서 Dictionary<TKey, TValue>를 파라미터로 받고 있습니다.

이런 형식은 XmlSerializer.Deserialize method와는 다른 형식이죠.

아래의 클래스는 XmlSerializer.Deserialize method와 비슷한 형식입니다.

즉 파라미터로는 XmlReader만 받고 있습니다.

그리고
Dictionary<TKey, TValue> 을 반환하고 있습니다.


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>>();
           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 Dictionary<TKey, TValue> Deserialize(XmlReader reader)
       {
           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<TKey, TValue> dictionary = new Dictionary<TKey, TValue>();
           foreach (Entry<TKey, TValue> item in entries)
           {
               dictionary[item.Key] = item.Value;
           }

           return dictionary;
       }
   }

   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"))
           {
               yourDictionary = mySerializer.Deserialize(reader);
           }

           foreach (KeyValuePair<string, int> item in yourDictionary)
           {
               Console.WriteLine(item.Key + " : " + item.Value.ToString());
           }
       }
   }
}

공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2025/05   »
1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31
글 보관함