XML基本操作-创建(DOM和LOINQ)和LINQ查询和保存

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 差不多,所以就不演示了。

猜你喜欢

转载自blog.csdn.net/hebizhi1997/article/details/80355114