SoFunction
Updated on 2025-03-07

Implementation code for reading elements and attribute values ​​in XML C#


  using System;
   using ;
   using ;
   using ;
   using ;
   namespace XMLReader
   {
     class XMLReader
     {
         static void Main(string[] args)
         {
             string xmlFilePath = "";
             XmlDocument doc = new XmlDocument();
             (xmlFilePath);

//School Use xpath expression to select all schoo children in the document
             XmlNodeList schoolNodeList = ("/school");
             if (schoolNodeList != null)
             {
                 foreach (XmlNode schoolNode in schoolNodeList)
                 {
//Get the attribute with the attribute name through Attributes
                     string schoolName = ["name"].Value;
("School:" + schoolName);

#region Grade
//Get the grades child nodes under the current node through the SelectSingleNode method
                     XmlNode gradesNode = ("grades");
                     if (gradesNode != null)
                     {
//Get all first-level child nodes of grades through the ChildNodes attribute
                         XmlNodeList gradeNodeList = ;
                         if (gradeNodeList != null)
                         {
                             foreach (XmlNode gradeNode in gradeNodeList)
                             {
                                 ("\t");
("Grade: " + ["name"].Value + "  ID:" + ["id"].Value);

#region Class
//Get the classes child nodes under the current node through the SelectSingleNode method
                                 XmlNode classesNode = ("classes");
                                 if (classesNode != null)
                                 {
//Get all first-level child nodes of classes through the ChildNodes attribute
                                     XmlNodeList classNodeList = ;
                                     if (classNodeList != null)
                                     {
                                         foreach (XmlNode classNode in classNodeList)
                                         {
("  Class: " + ["name"].Value + "    ID:" + ["id"].Value);

#region Teacher
                                             XmlNode teachersNode = ("teachers");
                                             if (teachersNode != null)
                                             {
                                                 XmlNodeList teacherNodeList = ;
                                                 if (teacherNodeList != null)
                                                 {
                                                     foreach (XmlNode teacherNode in teacherNodeList)
                                                     {                                              
                                                         XmlNode teacherNameNode = ;
                                                         XmlCDataSection cdate = (XmlCDataSection);
                                                         if (cdate != null)
                                                         {
("  "+["teach"].Value + "Teach: "+());                                                                                                                                                                                                                                                  �

                                                         }
                                                     }
                                                 }
                                             }
#endregion Teacher

#region All students
                                                 XmlNode studentsNode = ("students");
                                                 if (studentsNode != null)
                                                 {
                                                     XmlNodeList studentNodeList = ;
                                                     if (studentNodeList != null)
                                                     {
                                                         foreach (XmlNode studentNode in studentNodeList)
                                                         {
("                                                                                                                              �

//Get the student's attribute value name and text
                                                             XmlNode stu1 = ;
                                                             XmlElement xe1 = (XmlElement)stu1;
                                                             if (xe1 != null)
                                                             {
("                                                                                                                              �
                                                             }
//Get the student's attribute value sex and text
                                                             XmlNode stu2 = ;
                                                             XmlElement xe2 = (XmlElement)stu2;
                                                             if (xe2 != null)
                                                             {
("                                                                                                                              �
                                                             }
                                                         }
                                                     }
#endregion All students
                                                 }
                                             }
                                         }
#endregion Class
                                     }
                                 }
                             }
#endregion
("\r\nPress the random key to jump out");
                             ();
                         }

                     }
                 }
             }
         }
     }