Dictionary<,> 개체 직렬화
Post #7 ? Serializing Dictionary to/from XML
; http://blogs.msdn.com/mglehman/archive/2009/11/09/post-7-serializing-dictionary-string-string-to-from-xml.aspx
오호... Dictionary<,> 타입이 XmlSerializer 에서는 예외가 나더니만, DataContractSerializer 로는 직렬화가 잘 되는군요.
코드가 간단하니 아래에 실어봅니다.
static void Main(string[] args)
{
Dictionary<string, string> dict = new Dictionary<string, string>();
dict.Add("test", "test1");
MemoryStream ms = new MemoryStream();
XmlWriter writer = XmlWriter.Create(ms);
// 예외 발생
// XmlSerializer xs = new XmlSerializer(typeof(Dictionary<string, string>));
// xs.Serialize(ms, dict);
DataContractSerializer dcs = new DataContractSerializer(typeof(Dictionary<string, string>));
dcs.WriteObject(writer, dict);
writer.Close();
ms.Position = 0;
Dictionary<string, string> dict2 = dcs.ReadObject(ms) as Dictionary<string, string>;
}
[이 토픽에 대해서 여러분들과 의견을 공유하고 싶습니다. 틀리거나 미흡한 부분 또는 의문 사항이 있으시면 언제든 댓글 남겨주십시오.]