XML文档的增删改查

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

{

    public partial class Form1 : Form

    {

       public Form1()

       {

           InitializeComponent();

       }

 

       private void Form1_Load(object sender, EventArgs e)

       {

 

       }

       //创建XML文档

       private void btnCreate_Click(object sender, EventArgs e)

       {

           XmlDocument xmlDoc = new XmlDocument();

           //建立Xml的定义声明

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

           xmlDoc.AppendChild(dec);

           //创建根节点

           XmlElement root = xmlDoc.CreateElement("Books");

           xmlDoc.AppendChild(root);

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

           XmlNode book = xmlDoc.CreateElement("Book");

           //创建Book的子节点

           XmlElement title = xmlDoc.CreateElement("Title");

           title.InnerText = "SQLServer";

           book.AppendChild(title);

 

           XmlElement isbn = xmlDoc.CreateElement("ISBN");

            isbn.InnerText = "444444";

            book.AppendChild(isbn);

 

            XmlElement author = xmlDoc.CreateElement("Author");

            author.InnerText = "jia";

            book.AppendChild(author);

          

            XmlElement price = 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");

 

       }

       //插入新的节点

       private void btnInsert_Click(object sender, EventArgs e)

       {

           XmlDocument xmlDoc = new XmlDocument();

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

 

           XmlNode root = xmlDoc.SelectSingleNode("Books");

 

           XmlElement book = xmlDoc.CreateElement("Book");

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

          

 

           XmlElement title = xmlDoc.CreateElement("Title");

           title.InnerText = "XML";

           book.AppendChild(title);

 

           XmlElement isbn = xmlDoc.CreateElement("ISBN");

           isbn.InnerText = "555555";

           book.AppendChild(isbn);

 

           XmlElement author = xmlDoc.CreateElement("Author");

           author.InnerText = "fengyafei";

           book.AppendChild(author);

 

           XmlElement price = 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");

       }

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

       private void btnUpdate_Click(object sender, EventArgs e)

       {

           XmlDocument xmlDoc = new XmlDocument();

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

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

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

           //遍历所有子节点

           foreach (XmlNode node in list)

           {

              //将子节点类型转换为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");

       }

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

       private void btnDelete_Click(object sender, EventArgs e)

       {

           XmlDocument xmlDoc = new XmlDocument();

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

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

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

           //遍历所有子节点

           foreach (XmlNode node in 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文档

       private void btnRead_Click(object sender, EventArgs e)

       {

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

           XmlTextReader reader = new XmlTextReader(fileName);

           string str = 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写入(不是插入,写入会覆盖文档中已有的所有节点)节点

       private void btnWrite_Click(object sender, EventArgs e)

       {

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

            XmlTextWriter writer = 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();

 

       }

    }

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值