使用XmlDocument创建XML文档及增加删除更新节点

来源:互联网 发布:mac版office2016威锋 编辑:程序博客网 时间:2024/05/08 02:46

  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Text;
  7. using System.Windows.Forms;
  8. using System.Xml;
  9. namespace XMLDOMDemo
  10. {
  11.     public partial class Form1 : Form
  12.     {
  13.         public Form1()
  14.         {
  15.             InitializeComponent();
  16.         }
  17.         private void btnLoad_Click(object sender, EventArgs e)
  18.         {
  19.             XmlDocument xmlDoc = new XmlDocument();
  20.             xmlDoc.Load("Books.xml");
  21.             MessageBox.Show(xmlDoc.InnerXml);
  22.         }
  23.         //创建文档
  24.         private void btnCreate_Click(object sender, EventArgs e)
  25.         {
  26.             XmlDocument xmlDoc = new XmlDocument();
  27.             //建立Xml的定义声明
  28.             XmlDeclaration dec = xmlDoc.CreateXmlDeclaration("1.0""GB2312"null);
  29.             xmlDoc.AppendChild(dec);
  30.             //创建根节点
  31.             XmlElement root = xmlDoc.CreateElement("Books");
  32.             xmlDoc.AppendChild(root);
  33.             XmlNode book = xmlDoc.CreateElement("Book");
  34.             XmlElement title = xmlDoc.CreateElement("Title");
  35.             title.InnerText = "SQL Server";
  36.             book.AppendChild(title);
  37.             XmlElement isbn = xmlDoc.CreateElement("ISBN");
  38.             isbn.InnerText = "444444";
  39.             book.AppendChild(isbn);
  40.             XmlElement author = xmlDoc.CreateElement("Author");
  41.             author.InnerText = "jia";
  42.             book.AppendChild(author);
  43.             XmlElement price = xmlDoc.CreateElement("Price");
  44.             price.InnerText = "120";
  45.             price.SetAttribute("Unit", "___FCKpd___0quot;);
  46.             book.AppendChild(price);
  47.             root.AppendChild(book);
  48.             xmlDoc.Save("Books.xml");
  49.         }
  50.         private void btnInsert_Click(object sender, EventArgs e)
  51.         {
  52.             XmlDocument xmlDoc = new XmlDocument();
  53.             xmlDoc.Load("Books.xml");
  54.             XmlNode root = xmlDoc.SelectSingleNode("Books");
  55.             XmlElement book = xmlDoc.CreateElement("Book");
  56.             XmlElement title = xmlDoc.CreateElement("Title");
  57.             title.InnerText = "XML";
  58.             book.AppendChild(title);
  59.             XmlElement isbn = xmlDoc.CreateElement("ISBN");
  60.             isbn.InnerText = "333333";
  61.             book.AppendChild(isbn);
  62.             XmlElement author = xmlDoc.CreateElement("Author");
  63.             author.InnerText = "snow";
  64.             book.AppendChild(author);
  65.             XmlElement price = xmlDoc.CreateElement("Price");
  66.             price.InnerText = "120";
  67.             price.SetAttribute("Unit", "___FCKpd___0quot;);
  68.             book.AppendChild(price);
  69.             root.AppendChild(book);
  70.             xmlDoc.Save("Books.xml");
  71.             MessageBox.Show("数据已写入!");
  72.         }
  73.         private void btnUpdate_Click(object sender, EventArgs e)
  74.         {
  75.             XmlDocument xmlDoc = new XmlDocument();
  76.             xmlDoc.Load("Books.xml");
  77.             //"//Book[@Unit=/"$/"]"
  78.             //获取Books节点的所有子节点
  79.             XmlNodeList nodeList = xmlDoc.SelectSingleNode("Books//Book").ChildNodes;
  80.             //遍历所有子节点
  81.             foreach (XmlNode xn in nodeList)
  82.             {
  83.                 //将子节点类型转换为XmlElement类型
  84.                 XmlElement xe = (XmlElement)xn;
  85.                 if (xe.Name == "Author")
  86.                 {
  87.                     xe.InnerText = "amandag";
  88.                 }
  89.                 if (xe.GetAttribute("Unit") == "___FCKpd___0quot;)
  90.                 {
  91.                     xe.SetAttribute("Unit""¥");
  92.                 }
  93.                 //break;
  94.             }
  95.             //XmlNodeList nodeList = xmlDoc.SelectNodes("Books//Book"); 
  96.             //foreach (XmlNode xn in nodeList)
  97.             //{
  98.             //    foreach (XmlNode x in xn.ChildNodes)
  99.             //    {
  100.             //         //将子节点类型转换为XmlElement类型
  101.             //        XmlElement xe = (XmlElement)x;
  102.             //        if (xe.Name == "Author")
  103.             //        {
  104.             //            xe.InnerText = "amandag";
  105.             //        }
  106.             //        if (xe.GetAttribute("Unit") == "___FCKpd___0quot;)
  107.             //        {
  108.             //            xe.SetAttribute("Unit", "¥");
  109.             //        }
  110.             //        //break;                   
  111.             //    }
  112.             //}
  113.             xmlDoc.Save("Books.xml");
  114.         }
  115.         private void btnDelete_Click(object sender, EventArgs e)
  116.         {
  117.             XmlDocument xmlDoc = new XmlDocument();
  118.             xmlDoc.Load("Books.xml");
  119.             XmlNodeList nodeList = xmlDoc.SelectSingleNode("Books//Book").ChildNodes;
  120.             //遍历所有子节点
  121.             foreach (XmlNode xn in nodeList)
  122.             {
  123.                 //将子节点类型转换为XmlElement类型
  124.                 XmlElement xe = (XmlElement)xn;
  125.                 if (xe.Name == "Author")
  126.                 {
  127.                     xe.RemoveAll();
  128.                 }
  129.                 if (xe.GetAttribute("Unit") == "¥")
  130.                 {
  131.                     xe.RemoveAttribute("Unit");
  132.                 }
  133.             }
  134.             xmlDoc.Save("Books.xml");
  135.         }
  136.     }
  137. }





原创粉丝点击