XML基本操作-建立(DOM和LOINQ)和LINQ查詢和儲存

何畢之發表於2018-05-17
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using System.Xml.Linq;

namespace XML基本操作
{
    class Program
    {
        static void Main(string[] args)
        {
            CreateXMLDocByDOM();
            Console.WriteLine();
            CreateXMLDocByLINQ();
            Console.WriteLine();
            Console.WriteLine("呼叫QueryAttributeElement方法輸出結果:");
            QueryAttributeElement("f1.xml");
            Console.ReadKey();
        }

        static void CreateXMLDocByDOM()
        {
            XmlDocument doc = new XmlDocument();//建立一個XML文件
            XmlElement bookList = doc.CreateElement("BookList");//建立一個根節點BookList
            XmlElement book, auth;
            book = doc.CreateElement("Book");
            book.SetAttribute("Name", "Book-1");
            auth = doc.CreateElement("Author");
            auth.InnerText = "Author-1";
            book.AppendChild(auth);
            bookList.AppendChild(book);

            book = doc.CreateElement("Book");
            book.SetAttribute("Name", "Book-2");
            auth = doc.CreateElement("Author");
            auth.InnerText = "Author-2";
            book.AppendChild(auth);
            bookList.AppendChild(book);

            doc.AppendChild(bookList);
            doc.Save("f1.xml");
            Console.WriteLine(doc.InnerXml);//輸出到控制檯
            Console.WriteLine();
        }

        static void CreateXMLDocByLINQ()
        {
            XElement bookList = new XElement("BookList", new XElement[]
                {
                    new XElement("Book",new object[]
                    {
                        new XAttribute("Name","Book-1"),
                        new XElement("Author","Author-1")
                    }),
                    new XElement("Book",new object[]
                    {
                        new XAttribute("Name","Book-2"),
                        new XElement("Author","Author-2")
                    })
                });
            File.WriteAllText("f2.xml", bookList.ToString());
            Console.WriteLine(bookList);//輸出到控制檯
        }

        static void QueryAttributeElement(string path)
        {
            XElement root = XElement.Load(path);//載入path的資料到記憶體
            Console.WriteLine("查詢所有Book節點:");
            var userList =
                from ele in root.Elements("Book")//查詢所有Book節點
                select ele;
            foreach (var item in userList)
            {
                Console.WriteLine(item);
            }
            Console.WriteLine();
            Console.WriteLine("查詢所有Book節點的名字:");
            var nameList =
                from ele in root.Elements("Book")//查詢所有Book節點的名字
                select ((XAttribute)ele.Attribute("Name")).Value;
            foreach (var item in nameList)
            {
                Console.Write(item + " , ");
            }
        }
    }
}

LINQ to XML 的一系列操作和之前LINQ to DataTable 差不多,所以就不演示了。

相關文章