IT Story/C# & WPF
C# Tree, XML로 Save & Load
Hoyami7
2019. 12. 12. 21:55
반응형
TreeView 내용을 저장하고 다시 로드하는 기능을 구현할 일이 생겨서요.
저장 방법을 생각해보다 어차피 같은 트리 구조이니 Xml 사용해서 해보자 했는데
역시 생각처럼 간단하네요.
해당 소스는 공용이 아니라서 바로 쓰기에는 무리입니다. 참고만 하시고 쓰시는 용도에
맞춰서 약간 수정하시거나.. 아니면 참고만 하시면 됩니다.
TreeView 내용을 Xml로 저장하기 및 불러오기입니다.
public class XmlHandler
{
XmlDocument xmlDocument;
public XmlHandler()
{
}
public void TreeViewToXml(TreeView treeView, String path)
{
xmlDocument = new XmlDocument();
xmlDocument.AppendChild(xmlDocument.CreateElement("ROOT"));
XmlRekursivExport(xmlDocument.DocumentElement, treeView.Nodes);
xmlDocument.Save(path);
}
public void XmlToTreeView(String path, TreeView treeView)
{
xmlDocument = new XmlDocument();
xmlDocument.Load(path);
treeView.Nodes.Clear();
XmlRekursivImport(treeView.Nodes, xmlDocument.DocumentElement.ChildNodes);
}
private XmlNode XmlRekursivExport(XmlNode nodeElement, TreeNodeCollection treeNodeCollection)
{
XmlNode xmlNode = null;
foreach (TreeNode treeNode in treeNodeCollection)
{
xmlNode = xmlDocument.CreateElement("TreeViewNode");
xmlNode.Attributes.Append(xmlDocument.CreateAttribute("value"));
xmlNode.Attributes["value"].Value = treeNode.Text;
xmlNode.Attributes.Append(xmlDocument.CreateAttribute("n_Protocol"));
xmlNode.Attributes.Append(xmlDocument.CreateAttribute("NodType"));
xmlNode.Attributes.Append(xmlDocument.CreateAttribute("p_Protocol"));
xmlNode.Attributes.Append(xmlDocument.CreateAttribute("Auto_Rep"));
if(treeNode.Tag != null)
{
xmlNode.Attributes["n_Protocol"].Value = Convert.ToInt32(((NodeData)treeNode.Tag).n_Protocol).ToString();
xmlNode.Attributes["NodType"].Value = Convert.ToInt32(((NodeData)treeNode.Tag).nodeType).ToString();
xmlNode.Attributes["p_Protocol"].Value = Convert.ToInt32(((NodeData)treeNode.Tag).p_Protocol).ToString();
xmlNode.Attributes["Auto_Rep"].Value = Convert.ToInt32(((NodeData)treeNode.Tag).AUTO_Reply).ToString();
}
xmlNode.Attributes.Append(xmlDocument.CreateAttribute("ImgIdx"));
xmlNode.Attributes["ImgIdx"].Value = treeNode.ImageIndex.ToString();
xmlNode.Attributes.Append(xmlDocument.CreateAttribute("SelectedIdx"));
xmlNode.Attributes["SelectedIdx"].Value = treeNode.SelectedImageIndex.ToString();
if (nodeElement != null)
nodeElement.AppendChild(xmlNode);
if (treeNode.Nodes.Count > 0)
{
XmlRekursivExport(xmlNode, treeNode.Nodes);
}
}
return xmlNode;
}
private void XmlRekursivImport(TreeNodeCollection elem, XmlNodeList xmlNodeList)
{
TreeNode treeNode;
foreach (XmlNode myXmlNode in xmlNodeList)
{
treeNode = new TreeNode(myXmlNode.Attributes["value"].Value);
treeNode.Name = myXmlNode.Attributes["value"].Value;
if (myXmlNode.Attributes["NodType"].Value != "")
{
NodeData nd = new NodeData();
nd.nodeType = (NodeType)(Convert.ToInt32(myXmlNode.Attributes["NodType"].Value));
nd.p_Protocol = (EQ_PPROTOCOL)(Convert.ToInt32(myXmlNode.Attributes["p_Protocol"].Value));
nd.n_Protocol = (EQ_NPROTOCOL)(Convert.ToInt32(myXmlNode.Attributes["n_Protocol"].Value));
if ((Convert.ToInt32(myXmlNode.Attributes["Auto_Rep"].Value)) == 1)
nd.AUTO_Reply = true;
else nd.AUTO_Reply = false;
treeNode.Tag = nd;
}
treeNode.ImageIndex = Convert.ToInt32(myXmlNode.Attributes["ImgIdx"].Value);
treeNode.SelectedImageIndex = Convert.ToInt32(myXmlNode.Attributes["SelectedIdx"].Value);
if (myXmlNode.ChildNodes.Count > 0)
{
XmlRekursivImport(treeNode.Nodes, myXmlNode.ChildNodes);
}
elem.Add(treeNode);
}
}
}
반응형