2016年9月13日 星期二

[C#] 讀寫XML 並結合 DataTable


目的是設計XML格式並利用 DataTable, 寫入檔案 Default_Setting.xml

再讀取XML格式, 重新寫入 DataTable















using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

//DataTable 載入
using System.Data;
//XElement 載入
using System.Xml.Linq;
//讀取專案路徑
using System.Windows;
//XElement 載入
using System.Xml;

//using System.Data;

namespace XML_Example
{
    class Program
    {
        //讀取專案路徑
        static string ProjectPath = System.Environment.CurrentDirectory + "\\Default_Setting.xml";

        //建立新資料表
        static DataTable SourceDT = new DataTable("SourceDT");

        //讀取XML寫入資料表
        static DataTable NewDT = new DataTable("SourceDT");
     

        private static void Main(string[] args)
        {
            DataTableCreate();
            XMLCreate();
            ReadXML();
            Console.ReadLine();
        }

        /// <summary>
        /// 建立資料表提供XML儲存使用
        /// </summary>
        private static void DataTableCreate()
        {
         
            //建立資料格式
            SourceDT.Columns.Add("Name", typeof(string));
            SourceDT.Columns.Add("Chinese", typeof(string));
            SourceDT.Columns.Add("English", typeof(string));

            //新增資料
            //格式: 姓名 -> 國文成績 -> 英文成績 -> 數學成績
            SourceDT.Rows.Add("Kevin", "male", "ordinary");
            SourceDT.Rows.Add("Justin", "Female", "excellent");

            Console.WriteLine("原始DataTable資料");

            //DataTable table = GetTable(); // 取得資料表
            foreach (DataRow row in SourceDT.Rows) // 讀取行
            {
                Console.WriteLine("--- Row ---"); // Print separator.
                foreach (var item in row.ItemArray) // 讀取每個項目
                {
                    Console.Write("Item: "); // Print label.
                    Console.WriteLine(item); // Invokes ToString abstract method.
                }
            }
        }

        /// <summary>
        /// 利用datatable轉換為XML檔案
        /// </summary>
        private static void XMLCreate()
        {
            XElement Layer1 = new XElement("application");

            XElement Layer2 = new XElement("myClass");

            Layer2.Add(new XAttribute("Name", "Sheep"));
            Layer2.Add(new XElement("ClassRoom", "A-301"));

            foreach (DataRow row in SourceDT.Rows) // 讀取行
            {
                XElement Layer3 = new XElement("ClassMate");

                Layer3.Add(new XAttribute("Name", row[0].ToString()));
                Layer3.Add(new XElement("Gender", row[1].ToString()));
                Layer3.Add(new XElement("Score", row[2].ToString()));
                Layer2.Add(Layer3);
            }

            Layer1.Add(Layer2);

            XDocument xdoc = new XDocument();
            xdoc.Add(Layer1);
            xdoc.Save(ProjectPath);
        }

     

        private static void ReadXML()
        {
            //建立資料格式
            NewDT.Columns.Add("Name", typeof(string));
            NewDT.Columns.Add("Chinese", typeof(string));
            NewDT.Columns.Add("English", typeof(string));


            var allClassMates = XDocument.Load(ProjectPath).Root.Element("myClass").Elements("ClassMate");



            foreach (var person in allClassMates)
            {
                string name = person.Attribute("Name").Value;   // 屬性
                string gender = person.Element("Gender").Value;  // InnerText
                string score = person.Element("Score").Value;    // InnerText

                NewDT.Rows.Add(name, gender, score);
            }

            Console.WriteLine("\nXML讀取寫入DataTable");

            //DataTable table = GetTable(); // 取得資料表
            foreach (DataRow row in NewDT.Rows) // 讀取行
            {
                Console.WriteLine("--- Row ---"); // Print separator.
                foreach (var item in row.ItemArray) // 讀取每個項目
                {
                    Console.Write("Item: "); // Print label.
                    Console.WriteLine(item); // Invokes ToString abstract method.
                }
            }
        }
    }
}


XML檔案路徑:
[專案下]\XML_Example\XML_Example\bin\Debug\Default_Setting.xml


程式檔案:
Link

參考出處:
[C# 3.0] XLinq (Linq for XML Data)
[C#]Linq to XML 讀取XML檔


沒有留言:

張貼留言