The XML syntax

  1. There must be start and end tags
  2. Case sensitive
  3. There must be a root element
  4. Comments with

Create a studentsinfo.xml file for the test

<? XML version =' 1.0' encoding='UTF-8'? </Name> <Class>1</Class> </student> <student> <Name > <Class>3</Class> </student> <student> <Name ID="1003">xixi</Name> <Class>1</Class> </student> </Students>Copy the code

Create a Model

public class Student { public int ID { get; set; } public string Name { get; set; } public string Class { get; set; } public override string ToString() { return string.Format("ID:{0},Name:{1},Class:{2}", ID, Name, Class); }}Copy the code

Reading parsed XML

XmlDocument xmlDocument = new xmlDocument (); // Load the XML document xmlDocument.load (" studentsinfo.xml "); / / get the root node (Students) XmlNode rootNode. = xmlDocument SelectSingleNode ("/Students "); XmlNodeList stuNodeList = rootNode.ChildNodes; List<Student> stuList = new List<Student>(); foreach (XmlNode stuNode in stuNodeList) { Student stu = new Student(); XmlNodeList fieldNodeList = stuNode.childNodes; XmlNodeList fieldNodeList = stuNode.childNodes; foreach (XmlNode fieldNode in fieldNodeList) { if (fieldNode.Name == "Name") { stu.Name = fieldNode.InnerText; stu.ID = int.Parse(fieldNode.Attributes["ID"].Value); } else if (fieldNode.Name == "Class") { stu.Class = fieldNode.InnerText; } } stuList.Add(stu); } foreach (var stu in stuList) {console.writeline (stu.tostring ()); }Copy the code

Sutdent information can also be parsed using XmlElement

foreach (XmlNode stuNode in stuNodeList)
{
    Student stu = new Student();

    XmlElement nameEle = stuNode["Name"];
    stu.Name = nameEle.InnerText;
    stu.ID = int.Parse(nameEle.Attributes["ID"].Value);
    XmlElement classEle = stuNode["Class"];
    stu.Class = classEle.InnerText;

    stuList.Add(stu);
}
Copy the code

The output

Modify XML

XmlDocument xmlDocument = new xmlDocument (); // Load the XML document xmlDocument.load (" studentsinfo.xml "); XmlNode rootNode = xmlDocument.firstChild; XmlNodeList stuNodeList = rootNode.ChildNodes; List<Student> stuList = new List<Student>(); XmlElement nameEle = stuNodeList[0]["Name"]; // Change the node value nameEle.InnerText = "ha ha 2"; // Modify the attribute value nameele.setAttribute ("ID", "1005"); / / Save xmlDocument. Save (" StudentsInfo. XML ");Copy the code

Remember to Save after modification

Creating an XML file

// Create an xmlDocument object xmlDocument xmlDoc = new xmlDocument (); // Create the root node xmldoc.loadxml ("<? XML version =' 1.0' encoding='UTF-8'? ><Students></Students>"); // Get the Students node XmlElement Students = xmldoc.documentElement; Student XmlElement student = xmlDoc.createElement ("student"); // Create Name XmlElement stuName = xmlDoc.createElement ("Name"); Stuname. InnerText = "ha ha "; stuName.SetAttribute("ID", "1001"); student.AppendChild(stuName); // Create Class XmlElement stuClass = xmlDoc.createElement ("Class"); stuClass.InnerText = "1"; student.AppendChild(stuClass); Students.AppendChild(student); / / Save xmlDoc. Save (" StudentsInfo. XML ");Copy the code

The XML file

<? The XML version = "1.0" encoding = "utf-8"? </Name> <Class>1</Class> </student> </student>Copy the code