XML与TreeView

  1。treeview代码书写

        private void Form1_Load(object sender, EventArgs e)
        {
//代码为TreeView添加子项 treeView1.ContextMenuStrip = contextMenuStrip1; TreeNode tn = new TreeNode("默认"); treeView1.Nodes.Add(tn); } private void button1_Click(object sender, EventArgs e) {
//代码为TreeView添加子项 string text = textBox1.Text; TreeNode tn = new TreeNode(text); TreeNode tr = treeView1.SelectedNode; tr.Nodes.Add(tn); } private void 删除ToolStripMenuItem_Click(object sender, EventArgs e) {
//删除选中项 treeView1.SelectedNode.Remove(); }

 2.XML基本用法

     1XML文件



  



  LenovoY460

  5000
  酷睿i3
  
  



  

    Dell

    7000
    酷睿i7


  
  
  

  2 解析XML文件

  XmlDocument doc = new XmlDocument();
            doc.Load("XMLFile1.xml");
            XmlNode xn = doc.DocumentElement;
            foreach (XmlNode item in xn.ChildNodes) {

                //foreach (XmlNode item1 in item.ChildNodes) {

                //    switch (item1.Name) {
                    
                //        case "Name":
                //            Console.WriteLine("电脑的名字是:" + item1.InnerText);
                //            break;
                //        case "Price":
                //            Console.WriteLine("电脑的价格是:" + item1.InnerText);
                //            break;
                //        case "CPU":
                //              Console.WriteLine("电脑的CPU是:" + item1.InnerText);
                //            break;
                    
                   
                    
                //    }
                
                
                //}
                Console.WriteLine(item.Attributes["typr"].Value);
                Console.WriteLine("电脑的名字是:" + item["Name"].InnerText);
                Console.WriteLine("电脑的Price是:" + item["Price"].InnerText);
                Console.WriteLine("电脑的CPU是:" + item["CPU"].InnerText);
            }
            Console.ReadKey();

 

转载于:https://www.cnblogs.com/sunbin123/p/6590238.html

你可能感兴趣的:(XML与TreeView)