C#打开xml,并绑定TreeView

 

XML读取与写入


 这个代码有个问题就是XML一个节点的属性 xNode.Attributes不能显示出来

以这个优化了一下,见后篇


C#打开xml,并绑定TreeView_第1张图片C#打开xml,并绑定TreeView_第2张图片





//CODE

  //加载XML文件,并在TreeView上显示

        public void loadXML(String path)

       {

            try

            {

                this.Cursor =Cursors.WaitCursor;

                XmlDocument xDoc = newXmlDocument();

                xDoc.Load(path);

                XMLTree.Nodes.Clear();

                XMLTree.Nodes.Add(new   TreeNode(xDoc.DocumentElement.Name));

                TreeNode tNode = newTreeNode();

                tNode =(TreeNode)XMLTree.Nodes[0];

                addTreeNode(xDoc.DocumentElement,tNode);

                XMLTree.ExpandAll();

            }

            catch (XmlException xExc)

            {

                MessageBox.Show(xExc.Message);

            }

            catch (Exception ex)

            {

                MessageBox.Show(ex.Message);

            }

            finally

            {

                this.Cursor = Cursors.Default;//Change the cursor back

            }

        }

 

 

        private void addTreeNode(XmlNode xmlNode,TreeNode treeNode)

        {

            XmlNode xNode;

            TreeNode tNode;

            XmlNodeList xNodeList;

            if (xmlNode.HasChildNodes) //Thecurrent node has children

            {

                xNodeList = xmlNode.ChildNodes;

                for (int x = 0; x <=xNodeList.Count - 1; x++)

                {

                    xNode =xmlNode.ChildNodes[x];

                    //String str ="";

                    //if(xNode.Attributes!=null)

                    //{

                    //    for (int i = 0; i

                    //    {

                    //        str = str +xNode.Attributes.Item(i).Name;

                    //        str += "=";

                    //        str += xNode.Attributes.Item(i).Value;

                    //    }

                    //}

 

                    treeNode.Nodes.Add(newTreeNode(xNode.Name));

                    tNode = treeNode.Nodes[x];

                    addTreeNode(xNode, tNode);

                }

            }

            else

                treeNode.Text =xmlNode.OuterXml.Trim();

        }

 

 

 

 //保存XML文件

        private StreamWriter sr;

        public void exportToXml(stringfilename)

        {

            sr = new StreamWriter(filename,false, System.Text.Encoding.UTF8);

            //Write the header

            sr.WriteLine("");

            //Write our root node

            sr.WriteLine("<" +XMLTree.Nodes[0].Text + ">");

            foreach (TreeNode node inXMLTree.Nodes)

            {

                saveNode(node.Nodes);

            }

            //Close the root node

            sr.WriteLine("");

            sr.Close();

        }

        private voidsaveNode(TreeNodeCollection tnc)

        {

            foreach (TreeNode node in tnc)

            {

                //If we have child nodes, we'llwrite

                //a parent node, then iterratethrough

                //the children

                if (node.Nodes.Count > 0)

                {

                   sr.WriteLine("<" + node.Text + ">");

                    saveNode(node.Nodes);

                   sr.WriteLine("");

                }

                else //No child nodes, so wejust write the text

                    sr.WriteLine(node.Text);

            }

        }

 

你可能感兴趣的:(C#)