用C#读取xml有很多方式,这里我就先使用XmlDocument读取Xml,用一段代码遍历所有元素,并打印student的所有属性和子节点的值。
1、school.xml,如下:
<?xml version="1.0" encoding="UTF-8"?>
<!--启明培训学校学生信息:
1、一年级学生
2、二年级学生
-->
<school name="启明培训学校">
<grades>
<grade id="1" name="一年级">
<classes>
<class id="101" name="一班">
<teachers>
<teacher teach="语文"><teacherName><![CDATA[王军]]></teacherName></teacher>
<teacher teach="数学"><teacherName><![CDATA[周杰]]></teacherName></teacher>
</teachers>
<students>
<student id="10101">
<name>亢阳</name>
<sex>女</sex>
</student>
<student id="10102">
<name>田镇南</name>
<sex>男</sex>
</student>
<student id="10103">
<name>刘嘉</name>
<sex>女</sex>
</student>
</students>
</class>
</classes>
</grade>
<grade id="2" name="二年级">
<classes>
<class id="201" name="二班">
<teachers>
<teacher teach="语文"><teacherName><![CDATA[胡军]]></teacherName></teacher>
<teacher teach="数学"><teacherName><![CDATA[王洁]]></teacherName></teacher>
</teachers>
<students >
<student id="20101">
<name>胡艳</name>
<sex>女</sex>
</student>
<student id="20102">
<name>张三</name>
<sex>男</sex>
</student>
<student id="20103">
<name>李四</name>
<sex>女</sex>
</student>
</students>
</class>
</classes>
</grade>
</grades>
</school>
2、XMLReader.cs (我用的是控制台应用程序,直接附上全部代码吧)
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5
6 using System.Xml;
7
8 namespace XMLReader
9 {
10 class XMLReader
11 {
12 static void Main(string[] args)
13 {
14 string xmlFilePath = "school.xml";
15 XmlDocument doc = new XmlDocument();
16 doc.Load(xmlFilePath);
17
18 //学校 使用xpath表达式选择文档中所有的schoo的子节点
19 XmlNodeList schoolNodeList = doc.SelectNodes("/school");
20 if (schoolNodeList != null)
21 {
22 foreach (XmlNode schoolNode in schoolNodeList)
23 {
24 //通过Attributes获得属性名为name的属性
25 string schoolName = schoolNode.Attributes["name"].Value;
26 Console.WriteLine("学校:" + schoolName);
27
28 #region 年级
29 //通过SelectSingleNode方法获得当前节点下的grades子节点
30 XmlNode gradesNode = schoolNode.SelectSingleNode("grades");
31 if (gradesNode != null)
32 {
33 //通过ChildNodes属性获得grades的所有一级子节点
34 XmlNodeList gradeNodeList = gradesNode.ChildNodes;
35 if (gradeNodeList != null)
36 {
37 foreach (XmlNode gradeNode in gradeNodeList)
38 {
39 Console.WriteLine("\t");
40 Console.WriteLine("年级:" + gradeNode.Attributes["name"].Value + " ID:" + gradeNode.Attributes["id"].Value);
41
42 #region 班级
43 //通过SelectSingleNode方法获得当前节点下的classes子节点
44 XmlNode classesNode = gradeNode.SelectSingleNode("classes");
45 if (classesNode != null)
46 {
47 //通过ChildNodes属性获得classes的所有一级子节点
48 XmlNodeList classNodeList = classesNode.ChildNodes;
49 if (classNodeList != null)
50 {
51 foreach (XmlNode classNode in classNodeList)
52 {
53 Console.WriteLine(" 班级:" + classNode.Attributes["name"].Value + " ID:" + classNode.Attributes["id"].Value);
54
55 #region 老师
56 XmlNode teachersNode = classNode.SelectSingleNode("teachers");
57 if (teachersNode != null)
58 {
59 XmlNodeList teacherNodeList = teachersNode.ChildNodes;
60 if (teacherNodeList != null)
61 {
62 foreach (XmlNode teacherNode in teacherNodeList)
63 {
64 XmlNode teacherNameNode = teacherNode.FirstChild;
65 XmlCDataSection cdate = (XmlCDataSection)teacherNameNode.FirstChild;
66 if (cdate != null)
67 {
68 Console.WriteLine(" "+teacherNode.Attributes["teach"].Value + "老师:"+cdate.InnerText.Trim());
69
70 }
71 }
72 }
73 }
74 #endregion 老师
75
76 #region 所有学生
77 XmlNode studentsNode = classNode.SelectSingleNode("students");
78 if (studentsNode != null)
79 {
80 XmlNodeList studentNodeList = studentsNode.ChildNodes;
81 if (studentNodeList != null)
82 {
83 foreach (XmlNode studentNode in studentNodeList)
84 {
85 Console.WriteLine(" 学生:" + studentNode.Attributes["id"].Value);
86
87 //获取student的属性值name和文本
88 XmlNode stu1 = studentNode.FirstChild;
89 XmlElement xe1 = (XmlElement)stu1;
90 if (xe1 != null)
91 {
92 Console.WriteLine(" 姓名:" + xe1.InnerText.Trim());
93 }
94 //获取student的属性值sex和文本
95 XmlNode stu2 = studentNode.LastChild;
96 XmlElement xe2 = (XmlElement)stu2;
97 if (xe2 != null)
98 {
99 Console.WriteLine(" 姓别:" + xe2.InnerText.Trim());
100 }
101 }
102 }
103 #endregion 所有学生
104 }
105 }
106 }
107 #endregion 班级
108 }
109 }
110 }
111 #endregion 年级
112 Console.Write("\r\n按随意键跳出");
113 Console.ReadKey();
114 }
115
116 }
117 }
118 }
119 }
120 }
ok,运行,如图: