using System;
using System.IO;
using System.Xml;
namespace XML
{
class Program
{
static void Main(string[] args)
{
//string flePath = @"D:\715\CSharp\xml.txt";
//1.创建XML对象
XmlDocument xmlDoc = new XmlDocument();
//2.创建第一行文件信息,加入doc中
XmlDeclaration doc = xmlDoc.CreateXmlDeclaration("1.0", "utf-8", null);//版本,编码格式
xmlDoc.AppendChild(doc);
//3.创建根节点
XmlElement books = xmlDoc.CreateElement("Books");
books.SetAttribute("书目", "2.0");
xmlDoc.AppendChild(books);//将根节点添加到XML表中

        //4.创建根节点的子节点
        XmlElement book1 = xmlDoc.CreateElement("Book");
        books.AppendChild(book1);
        //创建子节点的子节点
        XmlElement name1 = xmlDoc.CreateElement("Name");
        XmlElement price1 = xmlDoc.CreateElement("Price");
        name1.InnerText = "Wei";
        price1.InnerText = "170";
        book1.AppendChild(name1);
        book1.AppendChild(price1);

        //根节点的另一个子节点
        XmlElement book2 = xmlDoc.CreateElement("Book");
        books.AppendChild(book2);

        XmlElement name2 = xmlDoc.CreateElement("Name");
        name2.InnerText = "ZZ";
        XmlElement price2 = xmlDoc.CreateElement("Price");
        price2.InnerText = "150";
        book2.AppendChild(name2);
        book2.AppendChild(price2);


        xmlDoc.Save(@"D:\715\CSharp\xml.txt");
        Console.WriteLine("保持成功");
        Console.ReadLine();

        /*//追加XML文档
        XmlDocument xmlDoc = new XmlDocument();
        XmlElement root;
        if(File.Exists(@"D:\715\CSharp\xml2.txt"))
        {
            //如存在则加载XML文档
            xmlDoc.Load(@"D:\715\CSharp\xml2.txt");
            //获取根节点
            root = xmlDoc.DocumentElement;
            
        }
        else
        {
            XmlDeclaration doc = xmlDoc.CreateXmlDeclaration("1.0", "utf-8", null);//版本,编码格式
            xmlDoc.AppendChild(doc);
            //3.创建根节点
            root = xmlDoc.CreateElement("Books");
            root.SetAttribute("书目", "2.0");
            xmlDoc.AppendChild(root);//将根节点添加到XML表中
        }

        XmlElement book2 = xmlDoc.CreateElement("Book");
        root.AppendChild(book2);

        XmlElement name2 = xmlDoc.CreateElement("Name");
        name2.InnerText = "ZL";
        XmlElement price2 = xmlDoc.CreateElement("Price");
        price2.InnerText = "30000";
        book2.AppendChild(name2);
        book2.AppendChild(price2);
        xmlDoc.Save(@"D:\715\CSharp\xml2.txt");*/

        /*//读取XML文档
        XmlDocument xmlDoc = new XmlDocument();
        //加载文档获取根节点
        xmlDoc.Load(@"D:\715\CSharp\xml2.txt");
        XmlElement root = xmlDoc.DocumentElement;
        //获取子节点 返回子节点的集合
        XmlNodeList xnl = root.ChildNodes;

        foreach (XmlNode node in xnl)
        {
            Console.WriteLine(node.InnerText );
        }*/

        /*//Xpath:获取节点属性
        XmlDocument xmlDoc = new XmlDocument();
        xmlDoc.Load(@"D:\715\CSharp\xml2.xml");
        XmlNodeList nodes = xmlDoc.SelectNodes("/Books/Book");

        foreach (XmlNode node in nodes)
        {
            Console.WriteLine(node.Attributes["W"].Value);
        }*/

        /*//删除节点
        XmlDocument xmlDoc = new XmlDocument();
        xmlDoc.Load(@"D:\715\CSharp\xml2.txt");
        XmlNode sel = xmlDoc.SelectSingleNode("/Books/Book");
        sel.RemoveAll();
        xmlDoc.Save(@"D:\715\CSharp\xml2.xml");*/

    }
}

}