View Code
1 转载: 2 3 C# xml 增删查改实例 4 5 using System; 6 using System.Collections.Generic; 7 using System.ComponentModel; 8 using System.Data; 9 using System.Drawing; 10 using System.Linq; 11 using System.Text; 12 using System.Windows.Forms; 13 using System.Xml; 14 15 namespace Doxml 16 { 17 public partial class Form1 : Form 18 { 19 public Form1() 20 { 21 InitializeComponent(); 22 } 23 24 //插入节点 25 private void button1_Click(object sender, EventArgs e) 26 { 27 28 //<book genre="李赞红" ISBN="2-3631-4"> 29 // <title>CS从入门到精通</title> 30 // <author>候捷</author> 31 // <price>58.3</price> 32 //</book> 33 34 XmlDocument xmlDoc = new XmlDocument(); 35 xmlDoc.Load(@"D:\XMLFile.xml"); 36 XmlNode root = xmlDoc.SelectSingleNode("bookstore");//查找<bookstore> 37 38 XmlElement xe1 = xmlDoc.CreateElement("book");//创建一个<book>节点 39 xe1.SetAttribute("genre", "李赞红");//设置该节点genre属性 40 xe1.SetAttribute("ISBN", "2-3631-4");//设置该节点ISBN属性 41 42 XmlElement xesub1 = xmlDoc.CreateElement("title"); 43 xesub1.InnerText = "CS从入门到精通";//设置文本节点 44 //AppendChild 将指定的节点添加到该节点的子节点列表的末尾。 45 xe1.AppendChild(xesub1);//添加到<book>节点中 46 47 XmlElement xesub2 = xmlDoc.CreateElement("author"); 48 // InnerText 获取或设置节点及其所有子级的串联值。 如果该节点下面含有串联的子级则予以全部覆盖指定的文本内容 49 xesub2.InnerText = "候捷"; 50 51 xe1.AppendChild(xesub2); 52 XmlElement xesub3 = xmlDoc.CreateElement("price"); 53 xesub3.InnerText = "58.3"; 54 xe1.AppendChild(xesub3); 55 56 57 root.AppendChild(xe1);//添加到<bookstore>节点中 58 xmlDoc.Save(@"D:\XMLFile.xml"); 59 60 } 61 62 //修改节点 63 private void button2_Click(object sender, EventArgs e) 64 { 65 XmlDocument xmlDoc = new XmlDocument(); 66 xmlDoc.Load(@"D:\XMLFile.xml"); 67 XmlNodeList nodeList = xmlDoc.SelectSingleNode("bookstore").ChildNodes;//获取bookstore节点的所有子节点 68 foreach (XmlNode xn in nodeList)//遍历所有子节点 69 { 70 XmlElement xe = (XmlElement)xn;//将子节点类型转换为XmlElement类型 71 if (xe.GetAttribute("genre") == "李赞红")//如果genre属性值为“李赞红” 72 { 73 xe.SetAttribute("genre", "update李赞红");//则修改该属性为“update李赞红” 74 75 XmlNodeList nls = xe.ChildNodes;//继续获取xe子节点的所有子节点 76 foreach (XmlNode xn1 in nls)//遍历 77 { 78 XmlElement xe2 = (XmlElement)xn1;//转换类型 79 if (xe2.Name == "author")//如果找到 80 { 81 xe2.InnerText = "亚胜";//则修改 82 break;//找到退出来就可以了 83 } 84 } 85 break; 86 } 87 } 88 89 xmlDoc.Save(@"D:\XMLFile.xml");//保存。 90 } 91 //删除节点 92 private void button3_Click(object sender, EventArgs e) 93 { 94 XmlDocument xmlDoc = new XmlDocument(); 95 xmlDoc.Load(@"D:\XMLFile.xml"); 96 //XmlNodeList 排序的结点集合 ChildNodes 获取指定节点下的子节点 97 XmlNodeList xnl = xmlDoc.SelectSingleNode("bookstore").ChildNodes; 98 99 //XmlNode表示XML文档中的单个节点 100 foreach (XmlNode xn in xnl) 101 { 102 //将节点转化为一个元素 103 XmlElement xe = (XmlElement)xn; 104 //提取指定属性值的元素,予以删除属性 105 if (xe.GetAttribute("genre") == "fantasy") 106 { 107 //移除属性 108 xe.RemoveAttribute("genre");//删除genre属性 109 } 110 else if (xe.GetAttribute("genre") == "update李赞红") 111 { 112 //移除指定节点下的内容,包括其子节点 113 xe.RemoveAll();//删除该节点的全部内容 114 } 115 } 116 xmlDoc.Save(@"D:\XMLFile.xml"); 117 } 118 119 //提取内容 120 private void button4_Click(object sender, EventArgs e) 121 { 122 richTextBox1.Clear(); 123 richTextBox1.LoadFile(@"D:\XMLFile.xml",RichTextBoxStreamType.PlainText); 124 } 125 } 126 } 127 128 129 130 131 132