xml和TreeView
1。TreeView代码代码:
1 private void Form1_Load(object sender, EventArgs e) 2 {<br> //代码为TreeView添加子项 3 treeView1.ContextMenuStrip = contextMenuStrip1; 4 TreeNode tn = new TreeNode("默认"); 5 6 treeView1.Nodes.Add(tn); 7 8 } 9 10 private void button1_Click(object sender, EventArgs e) 11 {<br> //代码为TreeView添加子项 12 string text = textBox1.Text; 13 TreeNode tn = new TreeNode(text); 14 TreeNode tr = treeView1.SelectedNode; 15 tr.Nodes.Add(tn); 16 17 } 18 19 private void 删除ToolStripMenuItem_Click(object sender, EventArgs e) 20 {<br> //删除选中项 21 treeView1.SelectedNode.Remove(); 22 }
2.XML基本用法
1 <?xml version="1.0" encoding="utf-8" ?> 2 <Computers> 3 4 5 <Computer typr="222"> 6 7 <Name>LenovoY460</Name> 8 9 <Price>5000</Price> 10 <CPU>酷睿i3</CPU> 11 12 13 </Computer> 14 15 16 <Computer typr="2223"> 17 18 <Name>Dell</Name> 19 20 <Price>7000</Price> 21 <CPU>酷睿i7</CPU> 22 23 24 </Computer> 25 26 27 </Computers> 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
59 XmlDocument doc = new XmlDocument(); 60 doc.Load("XMLFile1.xml"); 61 XmlNode xn = doc.DocumentElement; 62 foreach (XmlNode item in xn.ChildNodes) { 63 64 //foreach (XmlNode item1 in item.ChildNodes) { 65 66 switch (item1.Name) { 67 68 case "Name": 69 Console.WriteLine("电脑的名字是:" + item1.InnerText); 70 break; 71 case "Price": 72 Console.WriteLine("电脑的价格是:" + item1.InnerText); 73 break; 74 case "CPU": 75 Console.WriteLine("电脑的CPU是:" + item1.InnerText); 76 break; 77 78 79 80 } 81 82 83 } 84 Console.WriteLine(item.Attributes["typr"].Value); 85 Console.WriteLine("电脑的名字是:" + item["Name"].InnerText); 86 Console.WriteLine("电脑的Price是:" + item["Price"].InnerText); 87 Console.WriteLine("电脑的CPU是:" + item["CPU"].InnerText); 88 } 89 Console.ReadKey();