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

来源:互联网 发布:牛鼻铣刀编程计算 编辑:程序博客网 时间:2024/04/30 15:52


[csharp] view plaincopy
  1. using System;  
  2. using System.Windows.Forms;  
  3. using System.Xml;  
  4.   
  5. namespace XMLDemo  
  6. {  
  7.     public partial class FrmDOM : Form  
  8.     {  
  9.         public FrmDOM()  
  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.             //xml文档  
  24.             XmlDocument xmlDoc = new XmlDocument();  
  25.             XmlDeclaration dec = xmlDoc.CreateXmlDeclaration("1.0""utf-8"null);  
  26.             xmlDoc.AppendChild(dec);  
  27.   
  28.             //创建根节点   
  29.             XmlElement root = xmlDoc.CreateElement("Books");  
  30.             xmlDoc.AppendChild(root);  
  31.   
  32.             //节点及元素  
  33.             XmlNode book = xmlDoc.CreateElement("Book");  
  34.             XmlElement title = GetXmlElement(xmlDoc, "Title""Window Form");  
  35.             XmlElement isbn = GetXmlElement(xmlDoc, "ISBN""111111");  
  36.             XmlElement author = GetXmlElement(xmlDoc, "Author""amandag");  
  37.             XmlElement price = GetXmlElement(xmlDoc, "Price""128.00");  
  38.             price.SetAttribute("Unit""¥");  
  39.   
  40.             book.AppendChild(title);  
  41.             book.AppendChild(isbn);  
  42.             book.AppendChild(author);  
  43.             book.AppendChild(price);  
  44.             root.AppendChild(book);  
  45.   
  46.             xmlDoc.Save("Books.xml");  
  47.             MessageBox.Show("数据已写入!");  
  48.         }  
  49.   
  50.         private void btnInsert_Click(object sender, EventArgs e)  
  51.         {  
  52.             XmlDocument xmlDoc = new XmlDocument();  
  53.             xmlDoc.Load("Books.xml");  
  54.   
  55.             XmlNode root = xmlDoc.SelectSingleNode("Books");  
  56.   
  57.             XmlElement book = xmlDoc.CreateElement("Book");  
  58.             XmlElement title = GetXmlElement(xmlDoc, "Title""ASP.NET");  
  59.             XmlElement isbn = GetXmlElement(xmlDoc, "ISBN""222222");  
  60.             XmlElement author = GetXmlElement(xmlDoc, "Author""moon");  
  61.             XmlElement price = GetXmlElement(xmlDoc, "Price""111.00");  
  62.             price.SetAttribute("Unit", "{1}quot;);  
  63.   
  64.             book.AppendChild(title);  
  65.             book.AppendChild(isbn);  
  66.             book.AppendChild(author);  
  67.             book.AppendChild(price);  
  68.             root.AppendChild(book);  
  69.   
  70.             xmlDoc.Save("Books.xml");  
  71.             MessageBox.Show("数据已插入!");  
  72.         }  
  73.   
  74.         private void btnUpdate_Click(object sender, EventArgs e)  
  75.         {  
  76.             XmlDocument xmlDoc = new XmlDocument();  
  77.             xmlDoc.Load("Books.xml");  
  78.   
  79.             //方法1:获取Books//Book节点的第一个子节点   
  80.             XmlNodeList nodeList = xmlDoc.SelectSingleNode("Books//Book").ChildNodes;  
  81.             XmlElement xe = null;  
  82.             //遍历所有子节点   
  83.             foreach (XmlNode xn in nodeList)  
  84.             {  
  85.                 //将子节点类型转换为XmlElement类型   
  86.                 xe = (XmlElement)xn;  
  87.                 if (xe.Name == "Author" && xe.InnerText == "amandag")  
  88.                 {  
  89.                     xe.InnerText = "高歌";  
  90.                 }  
  91.   
  92.                 if (xe.GetAttribute("Unit") == "¥")  
  93.                 {  
  94.                     xe.SetAttribute("Unit", "{1}quot;);  
  95.                 }  
  96.             }  
  97.   
  98.             //方法2:  
  99.             XmlNode node = xmlDoc.SelectSingleNode("Books//Book[Author=\"moon\"]//Author");  
  100.             if(node != null)  
  101.             {  
  102.                 node.InnerText = "宝贝";  
  103.             }  
  104.       
  105.             xmlDoc.Save("Books.xml");  
  106.             MessageBox.Show("数据已更新!");  
  107.         }  
  108.   
  109.         private void btnDelete_Click(object sender, EventArgs e)  
  110.         {  
  111.             XmlDocument xmlDoc = new XmlDocument();  
  112.             xmlDoc.Load("Books.xml");  
  113.   
  114.             XmlNodeList nodeList = xmlDoc.SelectNodes("Books//Book//Price[@Unit=\"$\"]");  
  115.   
  116.             //遍历所有子节点   
  117.             foreach (XmlNode xn in nodeList)  
  118.             {  
  119.                 XmlElement xe = (XmlElement)xn;  
  120.                 xe.RemoveAttribute("Unit");  
  121.             }  
  122.             xmlDoc.Save("Books.xml");  
  123.             MessageBox.Show("数据已删除!");  
  124.         }  
  125.   
  126.         private XmlElement GetXmlElement(XmlDocument doc, string elementName, string value)  
  127.         {  
  128.             XmlElement element = doc.CreateElement(elementName);  
  129.             element.InnerText = value;  
  130.             return element;  
  131.         }  
  132.     }  
  133. }  
[csharp] view plaincopy
  1. using System;  
  2. using System.Windows.Forms;  
  3. using System.Xml;  
  4.   
  5. namespace XMLDemo  
  6. {  
  7.     public partial class FrmDOM : Form  
  8.     {  
  9.         public FrmDOM()  
  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.             //xml文档  
  24.             XmlDocument xmlDoc = new XmlDocument();  
  25.             XmlDeclaration dec = xmlDoc.CreateXmlDeclaration("1.0""utf-8"null);  
  26.             xmlDoc.AppendChild(dec);  
  27.   
  28.             //创建根节点   
  29.             XmlElement root = xmlDoc.CreateElement("Books");  
  30.             xmlDoc.AppendChild(root);  
  31.   
  32.             //节点及元素  
  33.             XmlNode book = xmlDoc.CreateElement("Book");  
  34.             XmlElement title = GetXmlElement(xmlDoc, "Title""Window Form");  
  35.             XmlElement isbn = GetXmlElement(xmlDoc, "ISBN""111111");  
  36.             XmlElement author = GetXmlElement(xmlDoc, "Author""amandag");  
  37.             XmlElement price = GetXmlElement(xmlDoc, "Price""128.00");  
  38.             price.SetAttribute("Unit""¥");  
  39.   
  40.             book.AppendChild(title);  
  41.             book.AppendChild(isbn);  
  42.             book.AppendChild(author);  
  43.             book.AppendChild(price);  
  44.             root.AppendChild(book);  
  45.   
  46.             xmlDoc.Save("Books.xml");  
  47.             MessageBox.Show("数据已写入!");  
  48.         }  
  49.   
  50.         private void btnInsert_Click(object sender, EventArgs e)  
  51.         {  
  52.             XmlDocument xmlDoc = new XmlDocument();  
  53.             xmlDoc.Load("Books.xml");  
  54.   
  55.             XmlNode root = xmlDoc.SelectSingleNode("Books");  
  56.   
  57.             XmlElement book = xmlDoc.CreateElement("Book");  
  58.             XmlElement title = GetXmlElement(xmlDoc, "Title""ASP.NET");  
  59.             XmlElement isbn = GetXmlElement(xmlDoc, "ISBN""222222");  
  60.             XmlElement author = GetXmlElement(xmlDoc, "Author""moon");  
  61.             XmlElement price = GetXmlElement(xmlDoc, "Price""111.00");  
  62.             price.SetAttribute("Unit", "{1}quot;);  
  63.   
  64.             book.AppendChild(title);  
  65.             book.AppendChild(isbn);  
  66.             book.AppendChild(author);  
  67.             book.AppendChild(price);  
  68.             root.AppendChild(book);  
  69.   
  70.             xmlDoc.Save("Books.xml");  
  71.             MessageBox.Show("数据已插入!");  
  72.         }  
  73.   
  74.         private void btnUpdate_Click(object sender, EventArgs e)  
  75.         {  
  76.             XmlDocument xmlDoc = new XmlDocument();  
  77.             xmlDoc.Load("Books.xml");  
  78.   
  79.             //方法1:获取Books//Book节点的第一个子节点   
  80.             XmlNodeList nodeList = xmlDoc.SelectSingleNode("Books//Book").ChildNodes;  
  81.             XmlElement xe = null;  
  82.             //遍历所有子节点   
  83.             foreach (XmlNode xn in nodeList)  
  84.             {  
  85.                 //将子节点类型转换为XmlElement类型   
  86.                 xe = (XmlElement)xn;  
  87.                 if (xe.Name == "Author" && xe.InnerText == "amandag")  
  88.                 {  
  89.                     xe.InnerText = "高歌";  
  90.                 }  
  91.   
  92.                 if (xe.GetAttribute("Unit") == "¥")  
  93.                 {  
  94.                     xe.SetAttribute("Unit", "{1}quot;);  
  95.                 }  
  96.             }  
  97.   
  98.             //方法2:  
  99.             XmlNode node = xmlDoc.SelectSingleNode("Books//Book[Author=\"moon\"]//Author");  
  100.             if(node != null)  
  101.             {  
  102.                 node.InnerText = "宝贝";  
  103.             }  
  104.       
  105.             xmlDoc.Save("Books.xml");  
  106.             MessageBox.Show("数据已更新!");  
  107.         }  
  108.   
  109.         private void btnDelete_Click(object sender, EventArgs e)  
  110.         {  
  111.             XmlDocument xmlDoc = new XmlDocument();  
  112.             xmlDoc.Load("Books.xml");  
  113.   
  114.             XmlNodeList nodeList = xmlDoc.SelectNodes("Books//Book//Price[@Unit=\"$\"]");  
  115.   
  116.             //遍历所有子节点   
  117.             foreach (XmlNode xn in nodeList)  
  118.             {  
  119.                 XmlElement xe = (XmlElement)xn;  
  120.                 xe.RemoveAttribute("Unit");  
  121.             }  
  122.             xmlDoc.Save("Books.xml");  
  123.             MessageBox.Show("数据已删除!");  
  124.         }  
  125.   
  126.         private XmlElement GetXmlElement(XmlDocument doc, string elementName, string value)  
  127.         {  
  128.             XmlElement element = doc.CreateElement(elementName);  
  129.             element.InnerText = value;  
  130.             return element;  
  131.         }  
  132.     }  
  133. }  
0 0
原创粉丝点击