C# 笔记 XML基础

            //1.创建一个XML文档对象
            XmlDocument doc = new XmlDocument();
            //2.创建第一行描述信息
            XmlDeclaration dec = doc.CreateXmlDeclaration("1.0", "utf-8", null);
            //3.将创建的第一行数据添加到文档中
            doc.AppendChild(dec);
            //4.给文档添加根节点
            XmlElement books = doc.CreateElement("Books");
            //5.将根节点添加给文档对象
            doc.AppendChild(books);
            //6.给根节点添加子节点
            XmlElement book1 = doc.CreateElement("Book");
            //将子节点book1添加到根节点下
            books.AppendChild(book1);
            //7.给book1添加子节点
            XmlElement bookName1 = doc.CreateElement("BookName");
            //8.设置标签中显示的文本
            bookName1.InnerText = "水浒传";
            book1.AppendChild(bookName1);
            // InnerText 会将<>解析为&lt;&gt; InnerXml会之间将<>写入xml
            XmlElement author1 = doc.CreateElement("Author");
            author1.InnerText = "<Author>匿名</Author>";
            book1.AppendChild(author1);
            XmlElement price1 = doc.CreateElement("Price");
            price1.InnerXml = "<Author>匿名</Author>";
            book1.AppendChild(price1);
            XmlElement des1 = doc.CreateElement("Des");
            des1.InnerXml = "宋朝时期人民起义";
            book1.AppendChild(des1);
            doc.Save("Book.xml");
            XmlElement orderItem2 = doc.CreateElement("OrderItem");
            orderItem2.SetAttribute("Name", "雨衣");
            orderItem2.SetAttribute("Count", "1");
            items.AppendChild(orderItem2);
原文地址:https://www.cnblogs.com/wu-xin/p/13996427.html