XmlDocument二个创建xml应用实例

作者:袖梨 2022-06-25

这里主要讲xmldocument创建,删除,编辑,插入节点的xml操作。

xmldocument xmldoc = new xmldocument();
            xmlnode xmlnode;
            xmlelement xmlelem;
            xmlelement xmlelem2;
            xmltext xmltext;
         
            //加入xml的声明段落
            xmlnode = xmldoc.createnode(xmlnodetype.xmldeclaration, "", "");
            xmldoc.appendchild(xmlnode);
            //加入一个根元素
            xmlelem = xmldoc.createelement("", "root", "");
            xmltext = xmldoc.createtextnode("root text");
            xmlelem.appendchild(xmltext);
            xmldoc.appendchild(xmlelem);
            //加入另外一个元素
            xmlelem2 = xmldoc.createelement("sampleelement");
            xmlelem2 = xmldoc.createelement("", "sampleelement", "");
            xmltext = xmldoc.createtextnode("the text of the sample element");
            xmlelem2.appendchild(xmltext);
            xmldoc.childnodes.item(1).appendchild(xmlelem2);
            //保存创建好的xml文档
            try
            {
                xmldoc.save(" g:/yunli/srcloud.web/uploads/filehistory/data.xml");
            }
            catch (exception e)
            {
                //显示错误信息
                console.writeline(e.message);
            }
            console.readline();
        }

上面只是简单的创建,下面这个实例,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", "¥");
                }
                //break;
            }

            //xmlnodelist nodelist = xmldoc.selectnodes("books//book");
            //foreach (xmlnode xn in nodelist)
            //{
            //    foreach (xmlnode x in xn.childnodes)
            //    {
            //         //将子节点类型转换为xmlelement类型
            //        xmlelement xe = (xmlelement)x;
            //        if (xe.name == "author")
            //        {
            //            xe.innertext = "amandag";
            //        }

            //        if (xe.getattribute("unit") == "___fckpd___0quot;)
            //        {
            //            xe.setattribute("unit", "¥");
            //        }
            //        //break;                  
            //    }
            //}
            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");
        }
    }
}

相关文章

精彩推荐