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

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