XML文档的增删改查

来源:互联网 发布:mac文件夹图标大小 编辑:程序博客网 时间:2024/06/02 08:22

XML文档的增删改查

 

<!-- /* Font Definitions */ @font-face{font-family:宋体;panose-1:2 1 6 0 3 1 1 1 1 1;mso-font-alt:SimSun;mso-font-charset:134;mso-generic-font-family:auto;mso-font-pitch:variable;mso-font-signature:3 135135232 16 0 262145 0;}@font-face{font-family:新宋体;panose-1:2 1 6 9 3 1 1 1 1 1;mso-font-charset:134;mso-generic-font-family:modern;mso-font-pitch:fixed;mso-font-signature:3 135135232 16 0 262145 0;}@font-face{font-family:"/@新宋体";panose-1:2 1 6 9 3 1 1 1 1 1;mso-font-charset:134;mso-generic-font-family:modern;mso-font-pitch:fixed;mso-font-signature:3 135135232 16 0 262145 0;}@font-face{font-family:"/@宋体";panose-1:2 1 6 0 3 1 1 1 1 1;mso-font-charset:134;mso-generic-font-family:auto;mso-font-pitch:variable;mso-font-signature:3 135135232 16 0 262145 0;} /* Style Definitions */ p.MsoNormal, li.MsoNormal, div.MsoNormal{mso-style-parent:"";margin:0cm;margin-bottom:.0001pt;text-align:justify;text-justify:inter-ideograph;mso-pagination:none;font-size:10.5pt;mso-bidi-font-size:12.0pt;font-family:"Times New Roman";mso-fareast-font-family:宋体;mso-font-kerning:1.0pt;}p.MsoHeader, li.MsoHeader, div.MsoHeader{margin:0cm;margin-bottom:.0001pt;text-align:center;mso-pagination:none;tab-stops:center 207.65pt right 415.3pt;layout-grid-mode:char;border:none;mso-border-bottom-alt:solid windowtext .75pt;padding:0cm;mso-padding-alt:0cm 0cm 1.0pt 0cm;font-size:9.0pt;font-family:"Times New Roman";mso-fareast-font-family:宋体;mso-font-kerning:1.0pt;} /* Page Definitions */ @page{mso-page-border-surround-header:no;mso-page-border-surround-footer:no;}@page Section1{size:595.3pt 841.9pt;margin:1.0cm 1.0cm 1.0cm 1.0cm;mso-header-margin:42.55pt;mso-footer-margin:49.6pt;mso-paper-source:0;layout-grid:15.6pt;}div.Section1{page:Section1;}-->

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Text;

using System.Windows.Forms;

using System.Xml;//这里要添加空间引用

 

namespace XMLTest

{

    publicpartial class Form1 : Form

    {

       publicForm1()

       {

           InitializeComponent();

       }

 

       privatevoid Form1_Load(objectsender, EventArgs e)

       {

 

       }

       //创建XML文档

       privatevoid btnCreate_Click(objectsender, EventArgs e)

       {

           XmlDocumentxmlDoc = new XmlDocument();

           //建立Xml的定义声明

           XmlDeclarationdec = xmlDoc.CreateXmlDeclaration("1.0","GB2312", null);

           xmlDoc.AppendChild(dec);

           //创建根节点

           XmlElementroot = xmlDoc.CreateElement("Books");

           xmlDoc.AppendChild(root);

           //创建根节点的子节点Book

           XmlNodebook = xmlDoc.CreateElement("Book");

           //创建Book的子节点

           XmlElementtitle = xmlDoc.CreateElement("Title");

           title.InnerText = "SQLServer";

           book.AppendChild(title);

 

           XmlElementisbn = xmlDoc.CreateElement("ISBN");

            isbn.InnerText = "444444";

            book.AppendChild(isbn);

 

            XmlElementauthor = xmlDoc.CreateElement("Author");

            author.InnerText = "jia";

           book.AppendChild(author);

          

            XmlElementprice = xmlDoc.CreateElement("Price");

            price.InnerText = "120";

            price.SetAttribute("Unit", "-FCKpd-0quot");

            book.AppendChild(price);

           //Book添加到根节点上

            root.AppendChild(book);

           //保存文档(如果已经存在该文件,则更新之;如果没有,则创建该文件)

           xmlDoc.Save(@"F:/my files/project/产品说明/系统流程图/我的文档/讨论/Books.xml");

 

       }

       //插入新的节点

       privatevoid btnInsert_Click(objectsender, EventArgs e)

       {

           XmlDocumentxmlDoc = new XmlDocument();

           xmlDoc.Load(@"F:/my files/project/产品说明/系统流程图/我的文档/讨论/Books.xml");

 

           XmlNoderoot = xmlDoc.SelectSingleNode("Books");

 

           XmlElementbook = xmlDoc.CreateElement("Book");

           book.SetAttribute("face", "red");

          

 

           XmlElementtitle = xmlDoc.CreateElement("Title");

           title.InnerText = "XML";

           book.AppendChild(title);

 

           XmlElementisbn = xmlDoc.CreateElement("ISBN");

           isbn.InnerText = "555555";

           book.AppendChild(isbn);

 

           XmlElementauthor = xmlDoc.CreateElement("Author");

           author.InnerText = "fengyafei";

           book.AppendChild(author);

 

           XmlElementprice = xmlDoc.CreateElement("Price");

           price.InnerText = "100";

           price.SetAttribute("Unit", "-FCKpd-0quot");

           book.AppendChild(price);

 

           root.AppendChild(book);

 

           xmlDoc.Save(@"F:/my files/project/产品说明/系统流程图/我的文档/讨论/Books2.xml");

       }

       //更改节点、元素和属性

       privatevoid btnUpdate_Click(objectsender, EventArgs e)

       {

           XmlDocumentxmlDoc = new XmlDocument();

           xmlDoc.Load(@"F:/my files/project/产品说明/系统流程图/我的文档/讨论/Books.xml");

           //获取Books节点的所有子节点

           XmlNodeListlist = xmlDoc.SelectSingleNode("Books//Book").ChildNodes;

           //遍历所有子节点

           foreach(XmlNode node inlist)

           {

              //将子节点类型转换为XmlElement类型

              XmlElement xe = (XmlElement)node;

 

              if (xe.Name == "Author")

              {

                  xe.InnerText = "along";

              }

              if (xe.GetAttribute("Unit")== "-FCKpd-0quot")

              {

                  xe.SetAttribute("Unit", "$");

              }

           }

           //保存

           xmlDoc.Save(@"F:/my files/project/产品说明/系统流程图/我的文档/讨论/Books3.xml");

       }

       //删除节点、元素或属性

       privatevoid btnDelete_Click(objectsender, EventArgs e)

       {

           XmlDocumentxmlDoc = new XmlDocument();

           xmlDoc.Load(@"F:/my files/project/产品说明/系统流程图/我的文档/讨论/Books.xml");

           //获取Books节点的所有子节点

           XmlNodeListlist = xmlDoc.SelectSingleNode("Books//Book").ChildNodes;

           //遍历所有子节点

           foreach (XmlNode nodein list)

           {

              //将子节点类型转换为XmlElement类型

              XmlElement xe = (XmlElement)node;

 

              if (xe.Name == "Author")

              {

                  xe.RemoveAll();

              }

              if (xe.GetAttribute("Unit")== "-FCKpd-0quot")

              {

                  xe.RemoveAttribute("Unit");

                  break;//删除后直接退出来

              }

           }

           //保存

           xmlDoc.Save(@"F:/my files/project/产品说明/系统流程图/我的文档/讨论/Books4.xml");

       }

       //XmlTextReader读取XML文档

       privatevoid btnRead_Click(objectsender, EventArgs e)

       {

           stringfileName = @"F:/my files/project/产品说明/系统流程图/我的文档/讨论/Books.xml";

           XmlTextReaderreader = new XmlTextReader(fileName);

           stringstr = string.Empty;

 

           while(reader.Read())

           {

              if (reader.NodeType == XmlNodeType.Element)

              {

                  if (reader.LocalName.Equals("Title")|| reader.LocalName.Equals("Author"))

                  {

                     str +=reader.ReadString() + " ";

                  }

              }

           }

           MessageBox.Show(str);

       }

       //XmlTextWriter写入(不是插入,写入会覆盖文档中已有的所有节点)节点

       privatevoid btnWrite_Click(objectsender, EventArgs e)

       {

           stringfileName = @"F:/my files/project/产品说明/系统流程图/我的文档/讨论/Books.xml";

            XmlTextWriterwriter = new XmlTextWriter(fileName,null);

            writer.Formatting = Formatting.Indented;

            writer.Indentation = 6;

           writer.WriteStartDocument();

            writer.WriteStartElement("Books");

           writer.WriteStartElement("Book");

           writer.WriteElementString("Title","Window Form");

           writer.WriteElementString("ISBN","111111");

           writer.WriteElementString("Author","amandag");

 

            writer.WriteStartElement("Price");

           writer.WriteAttributeString("Unit","");

            writer.WriteValue("128.00");

           writer.WriteEndElement();

           writer.WriteEndElement();

           writer.WriteStartElement("Book");

           writer.WriteElementString("Title","ASP.NET");

 

           writer.WriteElementString("ISBN","222222");

           writer.WriteElementString("Author","moon");

           writer.WriteStartElement("Price");

            writer.WriteAttributeString("Unit", "___FCKpd___0quot");

            writer.WriteValue("111.00");

           writer.WriteEndElement();

           writer.WriteEndElement();

           writer.WriteEndElement();

           writer.WriteEndDocument();

            writer.Close();

 

       }

    }

}