贤人必须成人,成人才能达己.

共同进步

  :: :: 博问 :: 闪存 :: 新随笔 :: :: :: 管理 ::

class TreeView2Xml
    {
        private const string _SubFolder = "Xml";
        private const string _DecodeConfigName = "TreeView.xml";

        public static void Save(TreeView tv  )
        {
            try
            {
                XmlDocument doc = new XmlDocument();
                doc.LoadXml("<Menu></Menu>");
                XmlNode root = doc.DocumentElement;
                doc.InsertBefore(doc.CreateXmlDeclaration("1.0", "utf-8", "yes"), root);
                TreeNode2Xml(tv.Nodes, root);
                doc.Save(CommonString.BuildFullPathName(_SubFolder, _DecodeConfigName));
            }
            catch (Exception e)
            {
                throw e;
            }
        }
        public static void TreeNode2Xml(TreeNodeCollection treeNodes, XmlNode xmlNode)
        {
            XmlDocument doc = xmlNode.OwnerDocument;
            foreach (TreeNode treeNode in treeNodes)
            {
                XmlNode element = doc.CreateNode("element", "Item", "");
                XmlAttribute attr = doc.CreateAttribute("Title");
                attr.Value = treeNode.Text;
                element.Attributes.Append(attr);
                attr = doc.CreateAttribute("id");
                attr.Value = treeNode.Name;
                element.Attributes.Append(attr);
                //element.AppendChild(doc.CreateCDataSection(treeNode.Tag.ToString()));
                xmlNode.AppendChild(element);

                if (treeNode.Nodes.Count > 0)
                {
                    TreeNode2Xml(treeNode.Nodes, element);
                }
            }
        }
        public static void Read(TreeView tv)
        {
            try
            {
                XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.Load(CommonString.BuildFullPathName(_SubFolder, _DecodeConfigName));

                XmlNodeList xmlNodes = xmlDoc.DocumentElement.ChildNodes;

                tv.BeginUpdate();
                tv.Nodes.Clear();
                XmlNode2TreeNode(xmlNodes, tv.Nodes);
                tv.EndUpdate();
            }
            catch(Exception e)
            {
                throw e;
            }
        }
        public static void XmlNode2TreeNode(XmlNodeList xmlNode, TreeNodeCollection treeNode)
        {
            foreach (XmlNode var in xmlNode)
            {
                if (var.NodeType != XmlNodeType.Element)
                {
                    continue;
                }
                TreeNode newTreeNode = new TreeNode();
                newTreeNode.Text = var.Attributes["Title"].Value;
                newTreeNode.Name = var.Attributes["id"].Value;
                if (var.HasChildNodes)
                {
                    //if (var.ChildNodes[0].NodeType == XmlNodeType.CDATA)
                    //{
                    //    newTreeNode.Tag = var.ChildNodes[0].Value;
                    //}

                    XmlNode2TreeNode(var.ChildNodes, newTreeNode.Nodes);
                }
                treeNode.Add(newTreeNode);
            }
        }
    }
   

posted on 2008-09-10 10:06  adi  阅读(220)  评论(0编辑  收藏  举报