用XmlDocument创建XML文档

ZDNet软件频道 时间:2008-07-10 作者:闵刚 | 中国IT实验室 我要评论()
本文关键词:创建 document XML 软件
用XmlDocument创建XML文档

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 XMLDOMDemo
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void btnLoad_Click(object sender, EventArgs e)
        {
            XMLdocument XMLDoc = new XMLdocument();
            XMLDoc.Load("Books.XML");
            MessageBox.Show(XMLDoc.InnerXML);
        }
        //创建文档
        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);
            XMLNode book = XMLDoc.CreateElement("Book");
            XMLElement title = XMLDoc.CreateElement("Title");
            title.InnerText = "SQL Server";
            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);
            root.AppendChild(book);
            XMLDoc.Save("Books.XML");
        }

        private void btnInsert_Click(object sender, EventArgs e)
        {
            XMLdocument XMLDoc = new XMLdocument();
            XMLDoc.Load("Books.XML");
            XMLNode root = XMLDoc.SelectSingleNode("Books");
            XMLElement book = XMLDoc.CreateElement("Book");
            XMLElement title = XMLDoc.CreateElement("Title");
            title.InnerText = "XML";
            book.AppendChild(title);
            XMLElement isbn = XMLDoc.CreateElement("ISBN");
            isbn.InnerText = "333333";
            book.AppendChild(isbn);
            XMLElement author = XMLDoc.CreateElement("Author");
            author.InnerText = "snow";
            book.AppendChild(author);
            XMLElement price = XMLDoc.CreateElement("Price");
            price.InnerText = "120";
            price.SetAttribute("Unit", "___FCKpd___0quot;);
            book.AppendChild(price);
            root.AppendChild(book);
            XMLDoc.Save("Books.XML");
            MessageBox.Show("数据已写入!");
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            XMLdocument XMLDoc = new XMLdocument();
            XMLDoc.Load("Books.XML");
            //"//Book[@Unit="$"]"
            //获取Books节点的所有子节点
            XMLNodeList nodeList = XMLDoc.SelectSingleNode("Books//Book").ChildNodes;
            //遍历所有子节点
            foreach (XMLNode xn in nodeList)
            {
                //将子节点类型转换为XMLElement类型
                XMLElement xe = (XMLElement)xn;
                if (xe.Name == "Author")
                {
                    xe.InnerText = "amandag";
                }
                if (xe.GetAttribute("Unit") == "___FCKpd___0quot;)
                {
                    xe.SetAttribute("Unit", "¥");
                }
            }
            XMLDoc.Save("Books.XML");
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            XMLdocument XMLDoc = new XMLdocument();
            XMLDoc.Load("Books.XML");
            XMLNodeList nodeList = XMLDoc.SelectSingleNode("Books//Book").ChildNodes;
            //遍历所有子节点
            foreach (XMLNode xn in nodeList)
            {
                //将子节点类型转换为XMLElement类型
                XMLElement xe = (XMLElement)xn;
                if (xe.Name == "Author")
                {
                    xe.RemoveAll();
                }
                if (xe.GetAttribute("Unit") == "¥")
                {
                    xe.RemoveAttribute("Unit");
                }
            }
            XMLDoc.Save("Books.XML");
        }
    }
}

创建

document

XML

软件


百度大联盟认证黄金会员Copyright© 1997- CNET Networks 版权所有。 ZDNet 是CNET Networks公司注册服务商标。
中华人民共和国电信与信息服务业务经营许可证编号:京ICP证010391号 京ICP备09041801号-159
京公网安备:1101082134