星期四, 5月 03, 2018

[LINQ] 建立 XML 檔案

學習 LINQ to XML 筆記,該篇是紀錄如何利用 XDocument 和 XElement 來建立 XML 檔案

Employee Class
namespace Helper
{
    public class Employee
    {
        public int ID { get; set; }
        public string Name { get; set; }
        public bool Gender { get; set; }
        public int Salary { get; set; }
    }
}
DataHelper Class
namespace Helper
{
    public static class DataHelper
    {
        public static string XMLPath = @"D:\Temp\XMLDemo.xml";

        public static List<Employee> GetEmployees()
        {
            return new List<Employee>
            {
                new Employee {ID = 1, Name = "張三", Gender = false, Salary = 50000},
                new Employee {ID = 2, Name = "李四", Gender = true,  Salary = 45000},
                new Employee {ID = 3, Name = "王五", Gender = false, Salary = 65000},
                new Employee {ID = 4, Name = "丁六", Gender = true,  Salary = 28000}
            };
        }

        public static void XMLFileExist()
        {
            if (File.Exists(XMLPath) == false)
            {
                throw new FileNotFoundException(XMLPath);
            }
        }
    }
}


主程式
using Helper;
using System.Xml.Linq;

namespace _01_CreateXML
{
    class Program
    {
        static void Main(string[] args)
        {
            CreateXml_Simple();
            // CreateXML_Source();
        }

        private static void CreateXml_Simple()
        {
            var xDoc = new XDocument(
                new XDeclaration("1.0", "utf-8", "yes"),
                new XComment("Linq2XML"),
                new XElement("Employees",
                    new XElement("Employee", new XAttribute("ID", 1),
                        new XElement("Name", "張三"),
                        new XElement("Gender", false),
                        new XElement("Salary", 50000)),
                    new XElement("Employee", new XAttribute("ID", 2),
                        new XElement("Name", "李四"),
                        new XElement("Gender", true),
                        new XElement("Salary", 45000)),
                    new XElement("Employee", new XAttribute("ID", 3),
                        new XElement("Name", "王五"),
                        new XElement("Gender", true),
                        new XElement("Salary", 65000)),
                    new XElement("Employee", new XAttribute("ID", 4),
                        new XElement("Name", "丁六"),
                        new XElement("Gender", true),
                        new XElement("Salary", 28000))));

            xDoc.Save(DataHelper.XMLPath);
        }

        private static void CreateXML_Source()
        {
            List<Employee> EmployeeList = DataHelper.GetEmployees();

            var xDoc = new XDocument
            (
                new XElement
                (
                    "Employees",
                    EmployeeList.Select(s =>
                           new XElement("Employee", new XAttribute("ID", s.ID),
                                new XElement("Name", s.Name),
                                new XElement("Gender", s.Gender),
                                new XElement("Salary", s.Salary)))
                )
            );

            xDoc.Save(DataHelper.XMLPath);
        }
    }
}
[LINQ] 建立 XML

沒有留言:

張貼留言