XML - XML document creation, addition, deletion and modification query

1. The first way to create

            //1. Create an XML document
            XmlDocument doc = new XmlDocument();

            //2. Create the first line of description information
            XmlDeclaration dec = doc.CreateXmlDeclaration("1.0", "utf-8", null);
            //3. Add the created first line of description information to the document
            doc.AppendChild( dec);

            //4. Add root node
            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();

Create the result as follows:

Second, the second way to create

    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 int Id
        {
            get
            {
                return id;
            }

            set
            {
                id = value;
            }
        }

        public string Name
        {
            get
            {
                return name;
            }

            set
            {
                name = value;
            }
        }

        public int Age
        {
            get
            {
                return age;
            }

            set
            {
                age = value;
            }
        }

        public string Sex
        {
            get
            {
                return sex;
            }

            set
            {
                sex = value;
            }
        }

        public Student(int id, string name, int age, string sex)
        {
            this.id = id;
            this.name = name;
            this.age = age;
            this.sex = sex;
        }

        public Student()
        {

        }
    }

Create the result as follows:

Third, the addition of XML files

            XmlDocument doc = new XmlDocument();
            //First determine whether the file exists, if it exists, append it otherwise create a
            if (File.Exists("Student.xml"))
            {
                //Load
                doc.Load("Student.xml" );

                //Get the root node and add child nodes to the root node
                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 保存成功");

Fourth, the XML document

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324675441&siteId=291194637