C#中用後臺代碼實現向XML文檔中追加內容

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using System.IO;


namespace CreateXML
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //判斷是否存在XML文件
            XmlElement books;
            XmlDocument doc = new XmlDocument();
            if (File.Exists("Odrer.xml"))
            {
                //存在XML文件的話,加載該XML文件並拿到根節點
                doc.Load("Odrer.xml");
                books = doc.DocumentElement;
            }
            else
            {
                XmlDeclaration dec = doc.CreateXmlDeclaration("1.0", "utf-8", null);
                doc.AppendChild(dec);
                books = doc.CreateElement("Odrers");
            }
            //不管是存在xml文件還是不存在xml文件,最終的目的就是拿到xml文件,並給其根節點進行追加子節點
            XmlElement order1 = doc.CreateElement("Order");
            books.AppendChild(order1);


            XmlElement customerName1 = doc.CreateElement("CustomerName");
            customerName1.InnerText = "小白";
            order1.AppendChild(customerName1);


            XmlElement customerNumber1 = doc.CreateElement("CustomerNumber");
            customerNumber1.InnerText = "XB3490343";
            order1.AppendChild(customerNumber1);


            XmlElement items1 = doc.CreateElement("Items");
            order1.AppendChild(items1);


            XmlElement orderItem1 = doc.CreateElement("OrderItem");
            orderItem1.SetAttribute("Name", "鼠標");
            orderItem1.SetAttribute("Count", "101");
            items1.AppendChild(orderItem1);


            XmlElement orderItem2 = doc.CreateElement("OrderItem");
            orderItem2.SetAttribute("Name", "鍵盤");
            orderItem2.SetAttribute("Count", "12");
            items1.AppendChild(orderItem2);






            XmlElement order2 = doc.CreateElement("Order");
            books.AppendChild(order2);


            XmlElement customerName2 = doc.CreateElement("CustomerName");
            customerName2.InnerText = "小LV";
            order2.AppendChild(customerName2);


            XmlElement customerNumber2 = doc.CreateElement("CustomerNumber");
            customerNumber2.InnerText = "XLV1930394";
            order2.AppendChild(customerNumber2);


            XmlElement items2 = doc.CreateElement("Items");
            order2.AppendChild(items2);


            XmlElement orderItem3 = doc.CreateElement("OrderItem");
            orderItem3.SetAttribute("Name", "IPhone6");
            orderItem3.SetAttribute("Count", "22");
            items2.AppendChild(orderItem3);


            XmlElement orderItem4 = doc.CreateElement("OrderItem");
            orderItem4.SetAttribute("Name", "IWatch");
            orderItem4.SetAttribute("Count", "12");
            items2.AppendChild(orderItem4);


            doc.Save("Odrer.xml");
            MessageBox.Show("追加成功!");
        }
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章