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

来源:互联网 发布:数控铣床编程格式 编辑:程序博客网 时间:2024/04/29 15:35
  1. using System;  
  2. using System.Windows.Forms;  
  3. using System.Xml;   
  4.   
  5. namespace XMLDemo  
  6. {  
  7.     public partial class FrmXMLDOMDemo : Form  
  8.     {  
  9.         public FrmXMLDOMDemo()  
  10.         {  
  11.             InitializeComponent();  
  12.         }  
  13.   
  14.         private void btnLoad_Click(object sender, EventArgs e)  
  15.         {  
  16.             XmlDocument xmlDoc = new XmlDocument();  
  17.             xmlDoc.Load("Books.xml");  
  18.             MessageBox.Show(xmlDoc.InnerXml);  
  19.         }  
  20.   
  21.         private void btnCreate_Click(object sender, EventArgs e)  
  22.         {  
  23.             XmlDocument xmlDoc = new XmlDocument();  
  24.   
  25.             //建立Xml的定义声明   
  26.             XmlDeclaration dec = xmlDoc.CreateXmlDeclaration("1.0""GB2312"null);  
  27.             xmlDoc.AppendChild(dec);  
  28.   
  29.             //创建根节点   
  30.             XmlElement root = xmlDoc.CreateElement("Books");  
  31.             xmlDoc.AppendChild(root);  
  32.   
  33.             XmlNode book = xmlDoc.CreateElement("Book");  
  34.   
  35.             XmlElement title = xmlDoc.CreateElement("Title");  
  36.             title.InnerText = "SQL Server";  
  37.             book.AppendChild(title);  
  38.   
  39.             XmlElement isbn = xmlDoc.CreateElement("ISBN");  
  40.             isbn.InnerText = "444444";  
  41.             book.AppendChild(isbn);  
  42.   
  43.             XmlElement author = xmlDoc.CreateElement("Author");  
  44.             author.InnerText = "jia";  
  45.             book.AppendChild(author);  
  46.   
  47.             XmlElement price = xmlDoc.CreateElement("Price");  
  48.             price.InnerText = "120";  
  49.             price.SetAttribute("Unit""$");  
  50.   
  51.             book.AppendChild(price);  
  52.             root.AppendChild(book);  
  53.   
  54.             xmlDoc.Save("Books.xml");  
  55.         }  
  56.   
  57.         private void btnInsert_Click(object sender, EventArgs e)  
  58.         {  
  59.             XmlDocument xmlDoc = new XmlDocument();  
  60.             xmlDoc.Load("Books.xml");  
  61.   
  62.             XmlNode root = xmlDoc.SelectSingleNode("Books");  
  63.   
  64.             XmlElement book = xmlDoc.CreateElement("Book");  
  65.   
  66.             XmlElement title = xmlDoc.CreateElement("Title");  
  67.             title.InnerText = "XML";  
  68.             book.AppendChild(title);  
  69.   
  70.             XmlElement isbn = xmlDoc.CreateElement("ISBN");  
  71.             isbn.InnerText = "333333";  
  72.             book.AppendChild(isbn);  
  73.   
  74.             XmlElement author = xmlDoc.CreateElement("Author");  
  75.             author.InnerText = "moon";  
  76.             book.AppendChild(author);  
  77.   
  78.             XmlElement price = xmlDoc.CreateElement("Price");  
  79.             price.InnerText = "120";  
  80.             price.SetAttribute("Unit""¥");  
  81.   
  82.             book.AppendChild(price);  
  83.   
  84.             root.AppendChild(book);  
  85.   
  86.             xmlDoc.Save("Books.xml");  
  87.             MessageBox.Show("数据已写入!");  
  88.         }  
  89.   
  90.         private void btnUpdate_Click(object sender, EventArgs e)  
  91.         {  
  92.             XmlDocument xmlDoc = new XmlDocument();  
  93.             xmlDoc.Load("Books.xml");  
  94.   
  95.             //"//Book[@Unit=/"$/"]"   
  96.             //获取Books//Book节点的第一个子节点   
  97.             XmlNodeList nodeList = xmlDoc.SelectSingleNode("Books//Book").ChildNodes;  
  98.   
  99.             //遍历所有子节点   
  100.             foreach (XmlNode xn in nodeList)  
  101.             {  
  102.                 //将子节点类型转换为XmlElement类型   
  103.                 XmlElement xe = (XmlElement)xn;  
  104.                 if (xe.Name == "Author")  
  105.                 {  
  106.                     xe.InnerText = "amandag";  
  107.                 }  
  108.   
  109.                 if (xe.GetAttribute("Unit") == "$")  
  110.                 {  
  111.                     xe.SetAttribute("Unit""¥");  
  112.                 }  
  113.             }  
  114.   
  115.             //获取Books//Book节点的所有子节点   
  116.             //XmlNodeList nodeList = xmlDoc.SelectNodes("Books//Book");  
  117.             //foreach (XmlNode xn in nodeList)  
  118.             //{  
  119.             //    foreach (XmlNode x in xn.ChildNodes)  
  120.             //    {  
  121.             //        XmlElement xe = (XmlElement)x;  
  122.             //        if (xe.Name == "Author")  
  123.             //        {  
  124.             //            xe.InnerText = "amandag";  
  125.             //        }  
  126.   
  127.             //        if (xe.GetAttribute("Unit") == "$")  
  128.             //        {  
  129.             //            xe.SetAttribute("Unit", "¥");  
  130.             //        }  
  131.             //        break;   
  132.             //    }  
  133.             //}   
  134.             xmlDoc.Save("Books.xml");  
  135.         }  
  136.   
  137.         private void btnDelete_Click(object sender, EventArgs e)  
  138.         {  
  139.             XmlDocument xmlDoc = new XmlDocument();  
  140.             xmlDoc.Load("Books.xml");  
  141.   
  142.             XmlNodeList nodeList = xmlDoc.SelectSingleNode("Books//Book").ChildNodes;  
  143.   
  144.             //遍历所有子节点   
  145.             foreach (XmlNode xn in nodeList)  
  146.             {  
  147.                 //将子节点类型转换为XmlElement类型   
  148.                 XmlElement xe = (XmlElement)xn;  
  149.                 if (xe.Name == "Author")  
  150.                 {  
  151.                     xe.RemoveAll();  
  152.                 }  
  153.   
  154.                 if (xe.GetAttribute("Unit") == "¥")  
  155.                 {  
  156.                     xe.RemoveAttribute("Unit");  
  157.                 }  
  158.             }  
  159.             xmlDoc.Save("Books.xml");  
  160.         }  
  161.     }  
  162. }  
原创粉丝点击