C#操作XML的方法
添加命名空间:
using System.Xml;
1,先创建一个BookModel类
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace xmllianxi { class BookModel { /// <summary> /// 所对应的课程类型 /// </summary> private string bookType; public string BookType { get { return bookType; } set { bookType = value; } } /// <summary> /// 书所对应的ISBN号 /// </summary> private string bookISBN; public string BookISBN { get { return bookISBN; } set { bookISBN = value; } } /// <summary> /// 书名 /// </summary> private string bookName; public string BookName { get { return bookName; } set { bookName = value; } } /// <summary> /// 作者 /// </summary> private string bookAuthor; public string BookAuthor { get { return bookAuthor; } set { bookAuthor = value; } } /// <summary> /// 价格 /// </summary> private double bookPrice; public double BookPrice { get { return bookPrice; } set { bookPrice = value; } } /// <summary> /// flag /// </summary> private string bookFlag; public string BookFlag { get { return bookFlag; } set { bookFlag = value; } } } }
2,创建xml文件,及增删改查xml文件:
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; using System.Xml; namespace xmllianxi { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void button5_Click(object sender, EventArgs e)//创建xml { XmlDocument doc = new XmlDocument(); //加入XML的声明段落,<?xml version="1.0" encoding="gb2312"?> XmlDeclaration xmldecl; xmldecl = doc.CreateXmlDeclaration("1.0", "utf-8", null); doc.AppendChild(xmldecl); //加入一个根元素 XmlElement xe = doc.CreateElement("", "bookstore", ""); doc.AppendChild(xe); //加入另外一个元素 XmlNode xn1 = doc.SelectSingleNode("bookstore");//查找<bookstore> XmlElement xel1 = doc.CreateElement("book");//创建一个<book>节点 xel1.SetAttribute("Type", "必修课");//设置该节点genre属性 xel1.SetAttribute("ISBN", "1-1");//设置该节点ISBN属性 //创建子结点 XmlElement xelName1 = doc.CreateElement("title"); xelName1.InnerText = "C#从入门到精通"; XmlElement xelAuthor1 = doc.CreateElement("author"); xelAuthor1.InnerText = "张三"; XmlElement xelPrice1 = doc.CreateElement("price"); xelPrice1.InnerText = "30"; xel1.AppendChild(xelName1); xel1.AppendChild(xelAuthor1); xel1.AppendChild(xelPrice1); xn1.AppendChild(xel1); //加入另外一个元素 XmlNode xn2 = doc.SelectSingleNode("bookstore");//查找<bookstore> XmlElement xel2 = doc.CreateElement("book");//创建一个<book>节点 xel2.SetAttribute("Type", "必修课");//设置该节点genre属性 xel2.SetAttribute("ISBN", "1-2");//设置该节点ISBN属性 //创建子结点 XmlElement xelName2 = doc.CreateElement("title"); xelName2.InnerText = "C#基础"; XmlElement xelAuthor2 = doc.CreateElement("author"); xelAuthor2.InnerText = "李四"; XmlElement xelPrice2 = doc.CreateElement("price"); xelPrice2.InnerText = "20"; xel2.AppendChild(xelName2); xel2.AppendChild(xelAuthor2); xel2.AppendChild(xelPrice2); xn2.AppendChild(xel2); //加入另外一个元素 XmlNode xn3 = doc.SelectSingleNode("bookstore");//查找<bookstore> XmlElement xel3 = doc.CreateElement("book");//创建一个<book>节点 xel3.SetAttribute("Type", "必修课");//设置该节点genre属性 xel3.SetAttribute("ISBN", "1-3");//设置该节点ISBN属性 //创建子结点 XmlElement xelName3 = doc.CreateElement("title"); xelName3.InnerText = "Unity3D基础"; XmlElement xelAuthor3 = doc.CreateElement("author"); xelAuthor3.InnerText = "王五"; XmlElement xelPrice3 = doc.CreateElement("price"); xelPrice3.InnerText = "45"; xel3.AppendChild(xelName3); xel3.AppendChild(xelAuthor3); xel3.AppendChild(xelPrice3); xn3.AppendChild(xel3); //加入另外一个元素 XmlNode xn4 = doc.SelectSingleNode("bookstore");//查找<bookstore> XmlElement xel4 = doc.CreateElement("book");//创建一个<book>节点 xel4.SetAttribute("Type", "必修课");//设置该节点genre属性 xel4.SetAttribute("ISBN", "1-4");//设置该节点ISBN属性 //创建子结点 XmlElement xelName4 = doc.CreateElement("title"); xelName4.InnerText = "计算机基础"; XmlElement xelAuthor4 = doc.CreateElement("author"); xelAuthor4.InnerText = "赵六"; XmlElement xelPrice4 = doc.CreateElement("price"); xelPrice4.InnerText = "25"; xel4.AppendChild(xelName4); xel4.AppendChild(xelAuthor4); xel4.AppendChild(xelPrice4); xn4.AppendChild(xel4); //保存创建好的XML文档 doc.Save(@"\Book.xml"); Select(); } public void Select() { //使用XmlDocument读取XML文件 XmlDocument doc = new XmlDocument(); doc.Load(@"\Book.xml"); XmlNode xn = doc.SelectSingleNode("bookstore");// 得到根节点bookstore XmlNodeList nodelist = xn.ChildNodes;// 得到根节点的所有子节点 List<BookModel> bookModeList = new List<BookModel>(); foreach (XmlNode xn1 in nodelist) { BookModel bookModel = new BookModel(); XmlElement xe = (XmlElement)xn1;// 将节点转换为元素,便于得到节点的属性值 bookModel.BookType = xe.GetAttribute("Type").ToString();// 得到Type的属性值 bookModel.BookISBN = xe.GetAttribute("ISBN").ToString(); // 得到Book节点的所有子节点 XmlNodeList xml = xe.ChildNodes; bookModel.BookName = xml.Item(0).InnerText; bookModel.BookAuthor = xml.Item(1).InnerText; bookModel.BookPrice = Convert.ToDouble(xml.Item(2).InnerText); if (xml.Item(3) != null) { bookModel.BookFlag = xml.Item(3).InnerText; } bookModeList.Add(bookModel); } dataGridView1.DataSource = bookModeList; } private void button1_Click(object sender, EventArgs e) { Select(); } private void button2_Click(object sender, EventArgs e)//增加 { //加载文件并选出根结点 XmlDocument doc = new XmlDocument(); doc.Load(@"\Book.xml"); XmlNode xn = doc.SelectSingleNode("bookstore"); //用上面的方法,是向已有的文件上追加数据,如果想覆盖原有的所有数据,使用LoadXml方法 //直接把根结点选择出来了,后面不用SelectSingleNode方法选择根结点,直接创建结点即可 //XmlDocument doc = new XmlDocument(); //doc.LoadXml("<bookstore></bookstore>");//用这句话,会把以前的数据全部覆盖掉,只有你增加的数据 XmlElement xel = doc.CreateElement("book");//创建一个book结点 xel.SetAttribute("Type", "必修课");//设置节点属性 xel.SetAttribute("ISBN", "1-10"); //创建子结点 XmlElement xelName = doc.CreateElement("title"); xelName.InnerText = "数据库基础"; XmlElement xelAuthor = doc.CreateElement("author"); xelAuthor.InnerText = "李老头"; XmlElement xelPrice = doc.CreateElement("price"); xelPrice.InnerText = "28"; xel.AppendChild(xelName); xel.AppendChild(xelAuthor); xel.AppendChild(xelPrice); //最后把book结点挂接在根结点上,并保存整个文件 xn.AppendChild(xel); doc.Save(@"\Book.xml"); Select(); } private void button3_Click(object sender, EventArgs e)//删除 { XmlDocument doc = new XmlDocument(); doc.Load(@"\Book.xml"); XmlNode xn = doc.SelectSingleNode("bookstore");// 得到根节点bookstore XmlNodeList nodelist = xn.ChildNodes;// 得到根节点的所有子节点 for (int i = 0; i < nodelist.Count; i++) { XmlElement xe = (XmlElement)nodelist.Item(i); if (xe.GetAttribute("ISBN") == "1-10") { xn.RemoveChild(xe); if (i < nodelist.Count) i = i - 1; } } doc.Save(@"\Book.xml"); Select(); } private void button4_Click(object sender, EventArgs e)//修改 { XmlDocument doc = new XmlDocument(); doc.Load(@"\Book.xml"); XmlNodeList nodeList = doc.SelectSingleNode("bookstore").ChildNodes;//获取根节点的所有子节点 foreach (XmlNode xn in nodeList)//遍历所有子节点 { XmlElement xe = (XmlElement)xn; //将子节点类型转换为XmlElement类型 if (xe.GetAttribute("ISBN") == "1-4") { xe.SetAttribute("ISBN", "update 1-4"); XmlNodeList xnls = xe.ChildNodes;//继续获取xe子节点的所有子节点 foreach (XmlNode xn1 in xnls) { XmlElement xe2 = (XmlElement)xn1; if (xe2.Name == "author") { xe2.InnerText = "赵老头"; break; } } break; } } doc.Save(@"\Book.xml"); Select(); } private void button6_Click(object sender, EventArgs e)//添加一个子节点 { XmlDocument doc = new XmlDocument(); doc.Load(@"\Book.xml"); XmlNodeList nodeList = doc.SelectSingleNode("bookstore").ChildNodes;//获取根节点的所有子节点 foreach (XmlNode xn in nodeList)//遍历所有子节点 { XmlElement xe = (XmlElement)xn; //将子节点类型转换为XmlElement类型 //创建子结点 XmlElement xelFlag = doc.CreateElement("flag"); xelFlag.InnerText = "11111"; xe.AppendChild(xelFlag); } doc.Save(@"\Book.xml"); Select(); } private void button7_Click(object sender, EventArgs e)//删除一个子节点 { XmlDocument doc = new XmlDocument(); doc.Load(@"\Book.xml"); XmlNodeList nodeList = doc.SelectSingleNode("bookstore").ChildNodes;//获取根节点的所有子节点 foreach (XmlNode xn in nodeList) { XmlElement xe = (XmlElement)xn; XmlNodeList xnl = xe.ChildNodes;//获取xe子节点的所有子节点 foreach (XmlNode xn1 in xnl)//遍历 { XmlElement xe2 = (XmlElement)xn1;//转换类型 if (xe2.Name == "flag")//如果找到 { xe.RemoveChild(xe2);//则删除 } } } doc.Save(@"\Book.xml"); Select(); } } }