1、school.xml,如下:
复制代码 代码如下:
<?xml version="1.0" encoding="UTF-8"?>
<>
<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 (我用的是控制台应用程序,直接附上全部代码吧)
复制代码 代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
namespace XMLReader
{
class XMLReader
{
static void Main(string[] args)
{
string xmlFilePath = "school.xml";
XmlDocument doc = new XmlDocument();
doc.Load(xmlFilePath);
//学校 使用xpath表达式选择文档中所有的schoo的子节点
XmlNodeList schoolNodeList = doc.SelectNodes("/school");
if (schoolNodeList != null)
{
foreach (XmlNode schoolNode in schoolNodeList)
{
//通过Attributes获得属性名为name的属性
string schoolName = schoolNode.Attributes["name"].Value;
Console.WriteLine("学校:" + schoolName);
#region 年级
//通过SelectSingleNode方法获得当前节点下的grades子节点
XmlNode gradesNode = schoolNode.SelectSingleNode("grades");
if (gradesNode != null)
{
//通过ChildNodes属性获得grades的所有一级子节点
XmlNodeList gradeNodeList = gradesNode.ChildNodes;
if (gradeNodeList != null)
{
foreach (XmlNode gradeNode in gradeNodeList)
{
Console.WriteLine("t");
Console.WriteLine("年级:" + gradeNode.Attributes["name"].Value + " ID:" + gradeNode.Attributes["id"].Value);
#region 班级
//通过SelectSingleNode方法获得当前节点下的classes子节点
XmlNode classesNode = gradeNode.SelectSingleNode("classes");
if (classesNode != null)
{
//通过ChildNodes属性获得classes的所有一级子节点
XmlNodeList classNodeList = classesNode.ChildNodes;
if (classNodeList != null)
{
foreach (XmlNode classNode in classNodeList)
{
Console.WriteLine(" 班级:" + classNode.Attributes["name"].Value + " ID:" + classNode.Attributes["id"].Value);
#region 老师
XmlNode teachersNode = classNode.SelectSingleNode("teachers");
if (teachersNode != null)
{
XmlNodeList teacherNodeList = teachersNode.ChildNodes;
if (teacherNodeList != null)
{
foreach (XmlNode teacherNode in teacherNodeList)
{
XmlNode teacherNameNode = teacherNode.FirstChild;
XmlCDataSection cdate = (XmlCDataSection)teacherNameNode.FirstChild;
if (cdate != null)
{
Console.WriteLine(" "+teacherNode.Attributes["teach"].Value + "老师:"+cdate.InnerText.Trim());
}
}
}
}
#endregion 老师
#region 所有学生
XmlNode studentsNode = classNode.SelectSingleNode("students");
if (studentsNode != null)
{
XmlNodeList studentNodeList = studentsNode.ChildNodes;
if (studentNodeList != null)
{
foreach (XmlNode studentNode in studentNodeList)
{
Console.WriteLine(" 学生:" + studentNode.Attributes["id"].Value);
//获取student的属性值name和文本
XmlNode stu1 = studentNode.FirstChild;
XmlElement xe1 = (XmlElement)stu1;
if (xe1 != null)
{
Console.WriteLine(" 姓名:" + xe1.InnerText.Trim());
}
//获取student的属性值sex和文本
XmlNode stu2 = studentNode.LastChild;
XmlElement xe2 = (XmlElement)stu2;
if (xe2 != null)
{
Console.WriteLine(" 姓别:" + xe2.InnerText.Trim());
}
}
}
#endregion 所有学生
}
}
}
#endregion 班级
}
}
}
#endregion 年级
Console.Write("rn按随意键跳出");
Console.ReadKey();
}
}
}
}
}
}
ok,运行,如图:
【C#读取XML中元素和属性值的实现代码】相关文章: