星期日, 5月 06, 2018

[LINQ] XML Update

學習 LINQ to XML 筆記,該篇是紀錄如何利用 SetValue()、SetElementValue() 和 SetAttributeValue() 來對 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 _04_XMLUpdate
{
    class Program
    {
        static void Main(string[] args)
        {
            DataHelper.XMLFileExist();
            Update_SetValue();
            Update_SetElementValue("2", "9999");
            Update_SetElementValue("3", null);
            Update_SetAttributeValue();
        }

        private static void Update_SetValue()
        {
            XDocument xDoc = XDocument.Load(DataHelper.XMLPath);
            XElement xEle = xDoc.Element("Employees");

            if (xEle != null)
            {
                var Target = xEle.Elements("Employee")
                    .Where(w =>
                    {
                        XAttribute xAttr = w.Attribute("ID");
                        return xAttr != null && xAttr.Value == "1";
                    })
                    .Select(s => s.Element("Salary"))
                    .FirstOrDefault();

                if (Target != null)
                {
                    Target.SetValue(9999);
                }
            }
            xDoc.Save(DataHelper.XMLPath);
        }

        private static void Update_SetElementValue(string ID, string UpdateValue)
        {
            XDocument xDoc = XDocument.Load(DataHelper.XMLPath);
            XElement xEle = xDoc.Element("Employees");

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

                if (Target != null)
                {
                    // 設為 null 的話,會把 Salary 移除
                    Target.SetElementValue("Salary", UpdateValue);
                }
            }
            xDoc.Save(DataHelper.XMLPath);
        }

        private static void Update_SetAttributeValue()
        {
            XDocument xDoc = XDocument.Load(DataHelper.XMLPath);
            XElement xEle = xDoc.Element("Employees");

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

                if (Target != null)
                {
                    Target.SetAttributeValue("ID", 9999);
                }
            }
            xDoc.Save(DataHelper.XMLPath);
        }

    }
}
[LINQ] XML Update

沒有留言:

張貼留言