using System
using System.Collections .Generic
using System.Linq
using System.Text
using System.Threading .Tasks
using System.Xml
namespace 创建XML
{
class Program
{
static void Main(string[] args)
{
//通过代码来创建XML文档
//1. 引用命名空间
//2. 创建XML文档对象 元素包含节点,元素是所有,节点是标签Element>=Node
XmlDocument doc = new XmlDocument()
//3. 第一行的描述信息
XmlDeclaration dec = doc.CreateXmlDeclaration ("1.0" , "utf-8" , null)
doc.AppendChild (dec )
//4. 创建根节点 rootNode
XmlElement books = doc.CreateElement ("Books" )
//添加根节点到文件
doc.AppendChild (books)
//5. 给根节点Boooks创建子节点
XmlElement book1 = doc.CreateElement ("Book" )
//将book添加到根节点
books.AppendChild (book1)
//6. 给Book1添加子节点
XmlElement name1 = doc.CreateElement ("Name" )
name1.InnerText = "金瓶梅"
book1.AppendChild (name1)
XmlElement price1 = doc.CreateElement ("Price" )
price1.InnerText = "100"
book1.AppendChild (price1)
XmlElement des1 = doc.CreateElement ("Des" )
des1.InnerText = "文言文不好看"
book1.AppendChild (des1)
doc.Save ("Books.xml" )
Console.WriteLine ("保存成功" )
Console.ReadKey ()
}
}
}