[C#]透過XElement建立xml資料

  • 4123
  • 0
  • C#
  • 2012-10-15

[C#]透過XElement建立xml資料

XElement物件預設是以序列的方式處理xml資料,可以直接根據xml資料的階層結構,透過XElement物件建立資料

記得要引用這兩個命名空間

using System.Linq; 
using System.Xml.Linq;

以下是個簡單範例


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.Linq;

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

        private void button1_Click(object sender, EventArgs e)
        {
            XElement company =
    new XElement("Company",
        new XElement("Employee",
            new XElement("ID", "001"),
            new XElement("Name", "胖虎")),
        new XElement("Employee",
            new XElement("ID", "002"),
            new XElement("Name", "小夫")
     )
);
            textBox1.Text = company.ToString();
        }
    }
}

 


如有錯誤 歡迎指正