zoukankan      html  css  js  c++  java
  • C# xml显示到treeview及递归遍历、删除、添加节点(两层节点)

    本文参考此文,且演示数据采用此文提供样例数据:

    https://www.cnblogs.com/xiaoxiangfeizi/archive/2011/08/07/2120807.html

    显示:

     public void btnAddRoot_Click(object sender, EventArgs e)
            {
                XmlDocument xmlDoc = new XmlDocument();
                XmlReaderSettings settings = new XmlReaderSettings();
                settings.IgnoreComments = true;                      //忽略文档里的注释
                XmlReader reader = XmlReader.Create("Info.xml", settings);
                xmlDoc.Load(reader);
                reader.Close();    
                XmlNode xn = xmlDoc.SelectSingleNode("bookstore");
    
                XmlNodeList lxn = xn.ChildNodes;
                foreach (XmlNode xn1 in lxn)
                {
                    XmlElement xe = (XmlElement)xn1;
                    TreeNode chdnode = new TreeNode();
                    TreeNode sonnode1 = new TreeNode();
                    TreeNode sonnode2 = new TreeNode();
                    TreeNode sonnode3 = new TreeNode();
                    TreeNode sonnode4 = new TreeNode();
    
                    XmlNodeList xn2 = xe.ChildNodes;
    
                    chdnode.Text = xn2.Item(0).InnerText;
                    rootnode.Nodes.Add(chdnode);
    
                    sonnode1.Text = "ISBN号:"+xe.GetAttribute("ISBN").ToString();
                    chdnode.Nodes.Add(sonnode1);
    
                    sonnode2.Text = "书类:"+xe.GetAttribute("Type").ToString();
                    chdnode.Nodes.Add(sonnode2);
    
                    sonnode3.Text = "作者:"+xn2.Item(1).InnerText;
                    chdnode.Nodes.Add(sonnode3);
    
                    sonnode4.Text = "价格:"+xn2.Item(2).InnerText;
                    chdnode.Nodes.Add(sonnode4);
                }
                this.tvTest.Nodes.Add(rootnode);
                tvTest.Nodes[0].Expand();

    删除选中节点:

     private void btnDelNode_Click(object sender, EventArgs e)
            {
                TreeNode treeNode = tvTest.SelectedNode;            
                if (MessageBox.Show("确定删除?", "确认删除", MessageBoxButtons.OKCancel) == DialogResult.OK)
                {
                    treeNode.Remove();
                }             
            }

    遍历所有节点:

            private List<TreeNode> nodeList = new List<TreeNode>();
            private void TraverseNode(TreeNode node)
            {
                nodeList.Add(node);
                for (int i = 0; i < node.Nodes.Count; i++)
                {
                    TraverseNode(node.Nodes[i]);
                }
            }
    
            private void btnSearch_Click(object sender, EventArgs e)
            {
                for (int i = 0; i < tvTest.Nodes.Count; i++)
                {
                    TraverseNode(tvTest.Nodes[i]);
                }
                for (int j = 0; j < nodeList.Count; j++)
                {
                    MessageBox.Show(nodeList[j].Text, null, MessageBoxButtons.OKCancel);
                }
            }

    添加节点(与另一个窗体联动),采用委托方法:

    子窗体代码:

    namespace WindowsFormsApplication
    {
        public delegate void ChangeTextHandler(string str1, string str2, string str3, string str4, string str5);
        public partial class Form2 : Form
        {
            public static Form2 form2;
          
            public Form2()
            {
                InitializeComponent();
                form2 = this;
            }
    
            public event ChangeTextHandler ChangeText;
            private void button1_Click(object sender, EventArgs e)
            {
                 ChangeText(textBox1.Text, textBox2.Text, comboBox1.Text, textBox3.Text, textBox4.Text);
                 this.Close();
            }       
        }
    }

    添加信息窗体:

    主窗体代码:

         private void btnAddNode_Click(object sender, EventArgs e)
            {
                Form2 f2 = new Form2();
                f2.ChangeText += new ChangeTextHandler(Change_Text);
                f2.Show();
            }
            public void Change_Text(string str1, string str2, string str3, string str4, string str5)
            {
                TreeNode chdnode = new TreeNode();
                TreeNode sonnode1 = new TreeNode();
                TreeNode sonnode2 = new TreeNode();
                TreeNode sonnode3 = new TreeNode();
                TreeNode sonnode4 = new TreeNode();
                chdnode.Text  = str1;
                sonnode1.Text = "ISBN号:" + str2;
                sonnode2.Text = "书类:" + str3;
                sonnode3.Text = "作者:" + str4;
                sonnode4.Text = "价格:" + str5;
                rootnode.Nodes.Add(chdnode);
                chdnode.Nodes.Add(sonnode1);
                chdnode.Nodes.Add(sonnode2);
                chdnode.Nodes.Add(sonnode3);
                chdnode.Nodes.Add(sonnode4);
            }
  • 相关阅读:
    哈夫曼
    P1631序列合并
    PAT Mice and Rice
    ybt 1356 计算(calc)
    P2947 Look Up S
    electron主进程与渲染进程的通信方式
    自定义BufferedReader
    小程序云数据库查询数据用在其它任意地方(完美解决)
    html+js实现微信跳转遮罩层
    Java创建属于自己的二维码(完整版)
  • 原文地址:https://www.cnblogs.com/horizonhz/p/13540940.html
Copyright © 2011-2022 走看看