C#後臺代碼創建XML文檔





C#代碼:


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;


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


        private void btnCreate_Click(object sender, EventArgs e)
        {
            XmlDocument doc = new XmlDocument();
            //創建XML的版本和編碼
            XmlDeclaration xd = doc.CreateXmlDeclaration("1.0", "utf-8", null);
            doc.AppendChild(xd);

            //創建根節點
            XmlElement orders = doc.CreateElement("Orders");
            doc.AppendChild(orders);

            //創建一級子節點
            //創建第一個訂單
            XmlElement order1 = doc.CreateElement("Order");
            orders.AppendChild(order1);

            //創建二級子節點
            XmlElement customerName = doc.CreateElement("CustomerName");
            customerName.InnerText = "劉洋";
            order1.AppendChild(customerName);

            XmlElement customerNumber = doc.CreateElement("CustomerNumber");
            customerNumber.InnerText = "BJ1000001";
            order1.AppendChild(customerNumber);

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

            //創建三級帶屬性的子節點
            XmlElement orderItem1 = doc.CreateElement("OrderItem");
            orderItem1.SetAttribute("Name", "碼錶");
            orderItem1.SetAttribute("Count", "2");
            itemes.AppendChild(orderItem1);

            XmlElement orderItem2 = doc.CreateElement("OrderItem");
            orderItem2.SetAttribute("Name", "雨衣");
            orderItem2.SetAttribute("Count", "10");
            itemes.AppendChild(orderItem2);

            XmlElement orderItem3 = doc.CreateElement("OrderItem");
            orderItem3.SetAttribute("Name", "手套");
            orderItem3.SetAttribute("Count", "15");
            itemes.AppendChild(orderItem3);


            //創建第二個訂單
            XmlElement order2 = doc.CreateElement("Odeer");
            orders.AppendChild(order2);

            //創建第二個訂單的一級子節點
            XmlElement customerName2 = doc.CreateElement("CustomerName");
            customerName2.InnerText = "李四";
            order2.AppendChild(customerName2);

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

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

            XmlElement orderItem4 = doc.CreateElement("OrderItem");
            orderItem4.SetAttribute("Name", "水滸傳");
            orderItem4.SetAttribute("Count", "1");
            items2.AppendChild(orderItem4);

            XmlElement orderItem5 = doc.CreateElement("OrderItem");
            orderItem5.SetAttribute("Name", "紅樓夢");
            orderItem5.SetAttribute("Count", "5");
            items2.AppendChild(orderItem5);

            doc.Save("Odrer.xml");
            MessageBox.Show("XML文檔創建成功!");
        }
    }
}




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章