【13】XML

1.简介(可扩展标记语言)
XML优点:容易读懂;格式标准任何语言都内置了XML分析引擎,不用单独进行文件分析引擎的编写。
Xml就是用一种格式化的方式来存储数据,我们可以通过用记事本打开。
.net程序中的一些配置文件app.config、web.config文件都是xml文件。
XML语法规范:标签/节点(Tag/Node)、嵌套(Nest)、属性。标签要闭合,属性值要用""包围,标签可以互相嵌套
XML树,父节点、子节点、兄弟节点(siblings)
xml编写完成以后可以用浏览器来查看,如果写错了浏览器会提示。如果明明没错,浏览器还是提示错误,则可能是文件编码问题。

2.语法特点

严格区分大小写
有且只能有一个根节点
有开始标签必须有结束标签,除非自闭合:<Person/>
属性必须使用双引号
(可选)文档声明:<?xml version="1.0" encoding="utf-8"?>
注释:<!-- -->
注意编码问题,文本文件实际编码要与文档声明中的编码一致。

3.读取xml文件

using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Xml;

namespace TestConsole
{
    delegate void MyDel();
    class Program
    {
        static void Main(string[] args)
        {
            XmlDocument xmlDocument = new XmlDocument();
            xmlDocument.Load(@"f:1.xml");//加载xml文件
            XmlNodeList xmlNodeList=xmlDocument.DocumentElement.ChildNodes;
            foreach (XmlNode node in xmlNodeList)
            {
                XmlElement xmlElement = (XmlElement)node;
                string stuID=xmlElement.GetAttribute("StuID");
                Console.WriteLine("Student的StuID属性值为:"+stuID);
                XmlNode cnode = xmlElement.SelectSingleNode("StuName");
                string stuName = cnode.InnerText;
                Console.WriteLine("StuName的文本内容为:" + stuName);

            }
            Console.ReadKey();
        }
    }
}
# region xml文件
//<Person>
//  <Student StuID = "11" >
//    < StuName > 张三 </ StuName >
//  </ Student >
//  < Student StuID="22">
//    <StuName>李四</StuName>
//  </Student>
//</Person>
#endregion xml文件

4.创建xml文件

using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Xml;

namespace TestConsole
{
    delegate void MyDel();
    class Program
    {
        static void Main(string[] args)
        {
            Person[] persons = { new Person(1, "rupeng", 8), new Person(2, "baidu", 6) };
            XmlDocument doc = new XmlDocument();
            XmlElement ePersons = doc.CreateElement("Persons");
            doc.AppendChild(ePersons);//添加根节点
            foreach (Person person in persons)
            {
                XmlElement ePerson = doc.CreateElement("Person");
                ePerson.SetAttribute("id", person.Id.ToString());
                XmlElement eName = doc.CreateElement("Name");
                eName.InnerText = person.Name;
                XmlElement eAge = doc.CreateElement("Age");
                eAge.InnerText = person.Age.ToString();
                ePerson.AppendChild(eName);
                ePerson.AppendChild(eAge);
                ePersons.AppendChild(ePerson);
            }
            doc.Save("f:/1.xml");
            Console.WriteLine("创建成功");
            Console.ReadKey();
        }
    }
    class Person
    {
        public Person(int id, string name, int age)
        {
            this.Id = id;
            this.Name = name;
            this.Age = age;
        }
        public int Id { set; get; }
        public string Name { set; get; }
        public int Age { set; get; }
    }

}
原文地址:https://www.cnblogs.com/lolitagis02/p/8215549.html