星期六, 5月 05, 2018

[LINQ] XML Insert

學習 LINQ to XML 筆記,該篇是紀錄如何利用 Add()、AddFirst()、AddBeforeSelf() 和 AddAfterSelf() 來對 XML 檔案插入 Element 資料

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 _03_XMLInsert
{
    class Program
    {
        static void Main(string[] args)
        {
            DataHelper.XMLFileExist();

            Add();
            AddFirst();
            AddBeforeSelf();
            AddAfterSelf();
        }

        /// <summary>
        /// 在 Employees 內新增資料,新增在最後一筆
        /// </summary>
        private static void Add()
        {
            XDocument xDoc = XDocument.Load(DataHelper.XMLPath);
            XElement xEle = xDoc.Element("Employees");

            if (xEle != null)
            {
                xEle.Add(new XElement("Employee", new XAttribute("ID", 5),
                    new XElement("Name", "趙八"),
                    new XElement("Gender", false),
                    new XElement("Salary", 35000)
                    ));
            }
            xDoc.Save(DataHelper.XMLPath);
        }

        /// <summary>
        /// 在 Employees 內新增資料,新增在第一筆
        /// </summary>
        private static void AddFirst()
        {
            XDocument xDoc = XDocument.Load(DataHelper.XMLPath);
            XElement xEle = xDoc.Element("Employees");

            if (xEle != null)
            {
                xEle.AddFirst(new XElement("Employee", new XAttribute("ID", 6),
                    new XElement("Name", "洪一"),
                    new XElement("Gender", true),
                    new XElement("Salary", 38000)
                    ));
            }
            xDoc.Save(DataHelper.XMLPath);
        }

        /// <summary>
        /// 在 Employees 內特定資料前新增資料
        /// </summary>
        private static void AddBeforeSelf()
        {
            XDocument xDoc = XDocument.Load(DataHelper.XMLPath);
            XElement xEle = xDoc.Element("Employees");

            if (xEle != null)
            {
                XElement Target = xEle.Elements("Employee").Where(w =>
                {
                    XAttribute xAttre = w.Attribute("ID");
                    return xAttre != null && xAttre.Value == "5";
                }).FirstOrDefault();

                if (Target != null)
                {
                    Target.AddBeforeSelf(new XElement("Employee", new XAttribute("ID", 8),
                        new XElement("Name", "黃二"),
                        new XElement("Gender", false),
                        new XElement("Salary", 66000)));
                }
            }
            xDoc.Save(DataHelper.XMLPath);
        }

        /// <summary>
        /// 在 Employees 內特定資料後新增資料
        /// </summary>
        private static void AddAfterSelf()
        {
            XDocument xDoc = XDocument.Load(DataHelper.XMLPath);
            XElement xEle = xDoc.Element("Employees");

            if (xEle != null)
            {
                XElement Target = xEle.Elements("Employee").Where(w =>
                {
                    XAttribute xAttre = w.Attribute("ID");
                    return xAttre != null && xAttre.Value == "2";
                }).FirstOrDefault();

                if (Target != null)
                {
                    Target.AddAfterSelf(new XElement("Employee", new XAttribute("ID", 9),
                        new XElement("Name", "楊二"),
                        new XElement("Gender", false),
                        new XElement("Salary", 99000)));
                }
            }
            xDoc.Save(DataHelper.XMLPath);
        }
    }
}
[LINQ] XML Insert

沒有留言:

張貼留言