c#基础四

写入一个XML文件

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
namespace _03写入一个XML文件
{
    class Program
    {
        static void Main(string[] args)
        {
            //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);


            XmlElement author1 = doc.CreateElement("Author");
            author1.InnerText = "<authorName>匿名</authorName>";
            book1.AppendChild(author1);

            XmlElement price1 = doc.CreateElement("Price");
            price1.InnerXml = "<authorName>匿名</authorName>";
            book1.AppendChild(price1);

            XmlElement des1 = doc.CreateElement("Des");
            des1.InnerXml = "好看,顶!~!!!!";
            book1.AppendChild(des1);


            Console.WriteLine("保存成功");
            doc.Save("Book.xml");
            Console.ReadKey();
        }
    }
}
View Code

 

添加带属性的XML文档

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
namespace _04添加带属性的XML文档
{
    class Program
    {
        static void Main(string[] args)
        {
            XmlDocument doc = new XmlDocument();
            XmlDeclaration dec = doc.CreateXmlDeclaration("1.0", "utf-8", null);
            doc.AppendChild(dec);

            XmlElement order = doc.CreateElement("Order");
            doc.AppendChild(order);

            XmlElement customerName = doc.CreateElement("CustomerName");
            customerName.InnerXml = "刘洋";
            order.AppendChild(customerName);

            XmlElement orderNumber = doc.CreateElement("OrderNumber");
            orderNumber.InnerXml = "10000";
            order.AppendChild(orderNumber);


            XmlElement items = doc.CreateElement("Items");
            order.AppendChild(items);


            XmlElement orderItem1 = doc.CreateElement("OrderItem");
            orderItem1.SetAttribute("Name", "码表");
            orderItem1.SetAttribute("Count", "100001");
            items.AppendChild(orderItem1);

            XmlElement orderItem2 = doc.CreateElement("OrderItem");
            orderItem2.SetAttribute("Name", "雨衣");
            orderItem2.SetAttribute("Count", "1");
            items.AppendChild(orderItem2);



            doc.Save("Order.xml");
            Console.WriteLine("保存成功");
            Console.ReadKey();
        }
    }
}
View Code

 

文档对象模型

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
namespace _05文档对象模型
{
    class Program
    {
        static void Main(string[] args)
        {
            List<Student> list = new List<Student>();
            list.Add(new Student() { ID = 1, Name = "yhb", Gender = '', Age = 30 });
            list.Add(new Student() { ID = 2, Name = "wl", Gender = '', Age = 20 });
            list.Add(new Student() { ID = 3, Name = "刘德华", Gender = '', Age = 50 });
            list.Add(new Student() { ID = 4, Name = "张学友", Gender = '', Age = 60 });
            list.Add(new Student() { ID = 5, Name = "哥哥", Gender = '', Age = -10 });


            XmlDocument doc = new XmlDocument();
            XmlDeclaration dec = doc.CreateXmlDeclaration("1.0", "utf-8", null);

            doc.AppendChild(dec);


            XmlElement person = doc.CreateElement("Person");
            doc.AppendChild(person);

            //通过循环List集合,获得所有对象 以节点的形式添加到XML文档中
            for (int i = 0; i < list.Count; i++)
            {
                XmlElement student = doc.CreateElement("Student");
                student.SetAttribute("studentID", list[i].ID.ToString());
                XmlElement name = doc.CreateElement("Name");
                name.InnerXml = list[i].Name;
                XmlElement age = doc.CreateElement("Age");
                age.InnerXml = list[i].Age.ToString();
                XmlElement gender = doc.CreateElement("Gender");
                gender.InnerXml = list[i].Gender.ToString();
                //添加
                person.AppendChild(student);
                student.AppendChild(name);
                student.AppendChild(age);
                student.AppendChild(gender);


            }

            doc.Save("Student.xml");
            Console.WriteLine("保存成功");
            Console.ReadKey();

        }
    }

    class Student
    {
        public int Age { get; set; }
        public string Name { get; set; }
        public int ID { get; set; }
        public char Gender { get; set; }
    }
}
View Code

 

对xml文档实现增删改查

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Xml;
namespace _06对xml文档实现增删改查
{
    class Program
    {
        static void Main(string[] args)
        {
            //XMLDocument
            #region 对xml文档实现追加的需求
            //XmlDocument doc = new XmlDocument();
            ////首先判断xml文档是否存在 如果存在 则追加  否则创建一个
            //if (File.Exists("Student.xml"))
            //{
            //    //加载进来
            //    doc.Load("Student.xml");
            //    //追加
            //    //获得根节点 给根节点添加子节点
            //    XmlElement person = doc.DocumentElement;

            //    XmlElement student = doc.CreateElement("Student");
            //    student.SetAttribute("studentID", "10");
            //    person.AppendChild(student);

            //    XmlElement name = doc.CreateElement("Name");
            //    name.InnerXml = "我是新来哒";
            //    student.AppendChild(name);

            //    XmlElement age = doc.CreateElement("Age");
            //    age.InnerXml = "18";
            //    student.AppendChild(age);

            //    XmlElement gender = doc.CreateElement("Gender");
            //    gender.InnerXml = "女";
            //    student.AppendChild(gender);

            //}
            //else
            //{
            //    //不存在

            //    XmlDeclaration dec = doc.CreateXmlDeclaration("1.0", "utf-8", null);
            //    doc.AppendChild(dec);

            //    XmlElement person = doc.CreateElement("Person");
            //    doc.AppendChild(person);


            //    XmlElement student = doc.CreateElement("Student");
            //    student.SetAttribute("studentID", "110");
            //    person.AppendChild(student);

            //    XmlElement name = doc.CreateElement("Name");
            //    name.InnerXml = "张三三李思思";
            //    student.AppendChild(name);

            //    XmlElement age = doc.CreateElement("Age");
            //    age.InnerXml = "28";
            //    student.AppendChild(age);

            //    XmlElement gender = doc.CreateElement("Gender");
            //    gender.InnerXml = "男";
            //    student.AppendChild(gender);
            //}

            //doc.Save("Student.xml");
            //Console.WriteLine("保存成功"); 
            #endregion


            #region 读取XML文档
            //XmlDocument doc = new XmlDocument();

            //doc.Load("OrDER.xml");

            ////还是 先获得根节点
            //XmlElement order = doc.DocumentElement;
            ////获得根节点下面的所有子节点
            //XmlNodeList xnl = order.ChildNodes;

            //foreach (XmlNode item in xnl)
            //{
            //    Console.WriteLine(item.InnerText);
            //}
            //XmlElement items = order["Items"];
            //XmlNodeList xnl2 = items.ChildNodes;
            //foreach (XmlNode item in xnl2)
            //{
            //    Console.WriteLine(item.Attributes["Name"].Value);
            //    Console.WriteLine(item.Attributes["Count"].Value);

            //    if (item.Attributes["Name"].Value == "手套")
            //    {
            //        item.Attributes["Count"].Value = "新来哒";
            //    }
            //}

            //doc.Save("OrDER.xml"); 
            #endregion



            #region 使用XPath的方式来读取XML文件


            //XmlDocument doc = new XmlDocument();
            //doc.Load("order.xml");
            ////获得根节点
            //XmlElement order = doc.DocumentElement;
            //XmlNode xn = order.SelectSingleNode("/Order/Items/OrderItem[@Name='雨衣']");

            ////Console.WriteLine(xn.Attributes["Name"].Value);

            //xn.Attributes["Count"].Value = "woshi new";


            //doc.Save("order.xml");
            //Console.WriteLine("保存成功");
            #endregion



            XmlDocument doc = new XmlDocument();

            doc.Load("order.xml");


            //doc.RemoveAll();不行 根节点不允许删除

            XmlElement order = doc.DocumentElement;

            //order.RemoveAll();移除根节点下的所有子节点

           // XmlNode xn = order.SelectSingleNode("/Order/Items/OrderItem[@Name='雨衣']");


           // //让orderItem去删除属性
           // XmlNode orderItem = order.SelectSingleNode("/Order/Items/OrderItem");

           // //获得Items节点

           // //XmlNode items = order["Items"];//order.SelectSingleNode("/Order/Items");

            
           //// items.RemoveChild(xn);移除当前节点
            
           // //orderItem.RemoveAttributeNode(xn.Attributes["Count"]);

           // xn.Attributes.RemoveNamedItem("Count");

           // doc.Save("order.xml");
           // Console.WriteLine("删除成功");
           // Console.ReadKey();


            //XPath
        }
    }
}
View Code

 

posted @ 2018-08-15 15:32  IT路上的小白  阅读(112)  评论(0编辑  收藏  举报