집 >백엔드 개발 >C#.Net 튜토리얼 >.net에서 XML을 TreeView로 변환
private static void CreateTree(TreeView treeView, String xmlfile) { xmlDoc = new XmlDocument(); xmlDoc.Load(xmlfile); try { numofChild = 0; treeView.Nodes.Clear(); XmlElement rootXml = xmlDoc.DocumentElement; TreeNode root = new TreeNode(); root.Text = rootXml.Name; treeView.Nodes.Add(root); if (rootXml.ChildNodes.Count > 0) { foreach (XmlNode subXmlnode in rootXml.ChildNodes) { TreeNode treenode = new TreeNode(); treenode.Text = "<" + subXmlnode.Name + ">"; root.Nodes.Add(treenode); AddNodeToTreeView(subXmlnode.ChildNodes, treenode); } } else { root.Text = rootXml.Value; } } catch (Exception ex) { throw ex; } } private static void AddNodeToTreeView(XmlNodeList xmlNodeList, TreeNode parentNode) { foreach (XmlNode xmlnode in xmlNodeList) { TreeNode subtreenode = new TreeNode(); subtreenode.Text = "<" + xmlnode.Name + ">"; parentNode.Nodes.Add(subtreenode); if (xmlnode.ChildNodes.Count > 0) { AddNodeToTreeView(xmlnode.ChildNodes, subtreenode); } else { subtreenode.Text = xmlnode.Value; } } }