首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > XML SOAP >

Xml文档增删节查

2012-12-24 
Xml文档增删改查 private void btnCreate_Click(object sender, EventArgs e){XmlDocument xmlDoc new X

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();

 

       }

    }

}

热点排行