티스토리 뷰
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());
}
}
}
}
앞의 예제에서는 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());
}
}
}
}
'Code > C#' 카테고리의 다른 글
Process Class Sample Code 1 (0) | 2008.01.14 |
---|---|
GenericDictionaryXmlSerializer 클래스 2 사용 예제 (0) | 2007.12.16 |
Generic Dictionary Xml Serialization C# Example 1 (0) | 2007.12.16 |
DateTime 이 포함된 객체의 Serialization (0) | 2007.12.15 |
List<string> 객체의 XML Serialization 사용 예제 (0) | 2007.12.15 |
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- jre
- autohotkey
- Service pack
- java
- AdSense감추기
- Rollback Rx
- WinAutomation
- 애드센스감추기
- Microsoft
- iText
- windows
- 스크린캡쳐
- Regular Expressions
- AxWebBrowser
- tagREADYSTATE
- .net framework
- Automation
- AdSense숨기기
- c#
- Phalanger
- 유틸리티
- registry
- ScreenHunter
- Sample Code
- 애드센스숨기기
- DotNetMagic
- READYSTATE_COMPLETE
- iTextSharp
- download.com
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
글 보관함