图解
1.代码案例
public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void tvList_AfterSelect(object sender, TreeViewEventArgs e) { } private TreeNode root; private void Form1_Load(object sender, EventArgs e) { //如何填充数据 //每一个节点都是什么类型? what type is it? //what's the type of TreeView Control every Node? //解析:TreeNode //游离的结合,和tvList控件没有半毛钱关系 root=new TreeNode("全国"); //why we can't see the whole world? //ImageList 与TreeView关联 tvList.ImageList = imgList; root.ImageIndex = 0; tvList.Nodes.Add(root); } private void btnAdd_Click(object sender, EventArgs e) { //1.1 在内存中构建出一个节点对象 TreeNode tn=new TreeNode(txtName.Text); //1.2 将子节点绑定到指定的全国节点下 //如何获取用户选中的节点 TreeNode selectNode=tvList.SelectedNode; selectNode.Nodes.Add(tn); } private void btnDel_Click(object sender, EventArgs e) { TreeNode selectNode = tvList.SelectedNode; selectNode.Remove(); } /////////////////////////////////////// using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace FrmMain { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void tvList_AfterSelect(object sender, TreeViewEventArgs e) { } private TreeNode root; private void Form1_Load(object sender, EventArgs e) { //如何填充数据 //每一个节点都是什么类型? what type is it? //what's the type of TreeView Control every Node? //解析:TreeNode //游离的结合,和tvList控件没有半毛钱关系 root = new TreeNode("父节点"); root.ImageIndex = 0; tvList.Nodes.Add(root); } private void btnAdd_Click(object sender, EventArgs e) { TreeNode tn = new TreeNode(textBox1.Text); TreeNode selectNode = tvList.SelectedNode; selectNode.Nodes.Add(tn); } private void button2_Click(object sender, EventArgs e) { TreeNode selectNode = tvList.SelectedNode; selectNode.Remove(); } } } /////////////////////////////////// <?xml version="1.0" encoding="utf-8" ?> <Books> <book id="001"> <name>假如生活期骗了你</name> <price>50</price> <publisher>北京大学出版社</publisher> </book> <book id="002"> <name>假如生活欺骗了你</name> <price>10</price> <publisher>清华大学出版社</publisher> </book> <book id="003"> <name>我与地坛史铁生</name> <price>50</price> <publisher>北大青鸟出版社</publisher> </book> </Books> ////////////////////////////// using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Xml; namespace Day08_我的xml项目 { class Program { static void Main(string[] args) { #region 解析xml文件 //我们需要将硬盘上名称为Happy.xml文件中的数据搁到内存,微软提供了 对应的操作类 //1.1文档对象创建 有内容里 XmlDocument doc = new XmlDocument(); //创建对象的目录,就是为了用他的方法 //1.2用Load方法加载xml文件,幻化成内存中的数据,赋值给doc对象 doc.Load("Happy.xml"); //1.3获取整篇文档 根节点 root XmlNode root = doc.DocumentElement; //1.4根据 根节点 获取子节点数据 foreach (XmlNode item in root.ChildNodes ) { //一个item代表一本书 foreach (XmlNode chid in item.ChildNodes ) { //一个child代表一个图书的属性 string name = chid.InnerText; Console.WriteLine(name); } } Console.ReadLine(); #endregion } } }