/* github */

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");
            doc.AppendChild(Books);
            XmlElement Book = doc.CreateElement("Book");
            Books.AppendChild(Book);
            XmlElement name = doc.CreateElement("name");
            name.InnerText = "水浒传";
            Book.AppendChild(name);
            XmlElement author = doc.CreateElement("author");
            author.InnerText = "匿名";
            author.SetAttribute("name", "wjl");
            author.SetAttribute("count", "30");
            Book.AppendChild(author);
            doc.Save("Book.xml");
            Console.WriteLine("保存成功!");
            Console.ReadKey();

 

创建结果如下:

                      

二、创建的第二种方式

   class Program
{              static void Main(string[] args)
{                  List<Student> list = new List<Student>();
                 list.Add(new Student(1, "wjl1", 22, ""));
                 list.Add(new Student(2, "wjl2", 21, ""));
                 list.Add(new Student(3, "wjl3", 22, ""));
                 list.Add(new Student(4, "wjl4", 24, ""));
                 XmlDocument xmldoc = new XmlDocument();
                 XmlDeclaration xmldec = xmldoc.CreateXmlDeclaration("1.0", "utf-8", null);
                xmldoc.AppendChild(xmldec);
                 XmlElement person = xmldoc.CreateElement("Person");
                xmldoc.AppendChild(person);
                 for (int i = 0; i < list.Count; i++)
                {
                     XmlElement stu = xmldoc.CreateElement("student");
                     stu.SetAttribute("ID", list[i].Id.ToString());
                    person.AppendChild(stu);
                     XmlElement name = xmldoc.CreateElement("name");
                     XmlElement age = xmldoc.CreateElement("age");
                     name.InnerText = list[i].Name;
                    age.InnerText = list[i].Age.ToString();
                   stu.AppendChild(name);
                    stu.AppendChild(age);
                }
                 xmldoc.Save("Student.xml");
                Console.WriteLine("Student.xml");
                Console.ReadKey();
            }
        }
         class Student
        {
            int id;
            string name;
            int age;
            string sex;
            public Student(int id, string name, int age, string sex)
            {
                 this.id = id;
                this.name = name;
               this.age = age;
               this.sex = sex;
           }
            public Student()
            {
           }
         }

 创建结果如下:

                     

三、对XML文件的添加

            XmlDocument doc = new XmlDocument();
            //首先判断文件是否存在,如果存在则追加否则在创建一个
            if (File.Exists("Student.xml"))
            {
                //加载
                doc.Load("Student.xml");
                //获取根节点,给根节点添加子节点
                XmlElement person = doc.DocumentElement;
                XmlElement student = doc.CreateElement("student");
                student.SetAttribute("ID", "1");
                person.AppendChild(student);
                XmlElement name = doc.CreateElement("name");
                XmlElement age = doc.CreateElement("age");
                name.InnerText = "zjs";
                age.InnerText = "41";
                student.AppendChild(name);
                student.AppendChild(age);
            }
            else {
            }
            doc.Save("Student.xml");
            Console.WriteLine("Student.xml 保存成功");

四、对XML文档的查询、修改、删除

方法1:

文档结构为:

                     

if (File.Exists("order.xml"))
            {
                doc.Load("order.xml");
                // 获取根节点
                XmlElement orderElement  = doc.DocumentElement;
                XmlNodeList orderChildr  = orderElement.ChildNodes;
                foreach (XmlNode item in orderChildr)
                {
                    Console.WriteLine("节点名称:"+ item.Name + "节点的 InnerText :" + item.InnerText);
                }
                XmlElement orderitem = orderElement["Items"];
                XmlNodeList itemlist = orderitem.ChildNodes;
                foreach (XmlNode item in itemlist)
                {
                    Console.WriteLine(item.Attributes["Name"].Value + " " + item.Attributes["Count"].Value);
                }
  }
            else
            {
                Console.WriteLine("文件不存在!");
            }
            Console.ReadKey();
           // doc.Save("Student.xml");
            Console.WriteLine("Student.xml 保存成功");
  

方法2:

            #region 使用XPath的方式来读取XML文件
            // 获取文档对象
            XmlDocument doc = new XmlDocument();
            doc.Load("order.xml");
            //获取根节点
            XmlElement order = doc.DocumentElement;
            // 获取单个节点
            //XmlNode xn = order.SelectSingleNode(@"/Order/CustomerName");
            XmlNode xn = order.SelectSingleNode(@"/Order/Items/OrderItem[@Name='码表']");
            xn.Attributes["Count"].Value = "20"; // 修改
            doc.Save("Order.xml");
            Console.WriteLine(xn.Attributes["Count"].Value);
            Console.ReadKey();
            #endregion

删除元素指定的特性:

            xn.Attributes.RemoveNamedItem("Count");  //删除元素指定的特性

删除子节点:

 

            XmlNode xn = order.SelectSingleNode(@"/Order/Items");
            XmlNode xnchild  = order.SelectSingleNode(@"/Order/Items/OrderItem[@Name = '雨衣']");
            xn.RemoveChild(xnchild); //删除指定的子节点

 

删除当前所有子节点:

            xn.RemoveAll(); //删除当前节点的所有子节点   

删除当前节点的所有特性:

            xnchild.Attributes.RemoveAll(); 
posted on 2018-04-21 20:24  王精灵  阅读(6849)  评论(3编辑  收藏  举报