Programing/C#
[C#] Dictionary를 Element로 변환후 XML로 저장
즐거운 개발자
2014. 11. 18. 16:41
반응형
Dictionary를 Xelement로 변환 한 후 , XML파일로 저장한다.
저장한 파일을 읽어서 Dictionary로 변환.
#소스코드
//Dictionary to Element:
Dictionary<string, string> dict = new Dictionary<string,string>();
XElement el = new XElement("root",dict.Select(kv => new XElement(kv.Key, kv.Value)));
el.Save("파일경로")
//Element to Dictionary:
XElement rootElement = XElement.Load("파일 경로");
Dictionary<string, string> dict = new Dictionary<string, string>();
foreach(var el in rootElement.Elements())
{
dict.Add(el.Name.LocalName, el.Value);
}