C#读取XML中元素和属性值的实现代码

 更新时间:2013年04月11日 15:58:34   作者:  
用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 (我用的是控制台应用程序,直接附上全部代码吧) 
复制代码 代码如下:

  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("\r\n按随意键跳出");
                             Console.ReadKey();
                         }

                     }
                 }
             }
         }
     }


ok,运行,如图:

相关文章

  • asp.net获取系统当前时间的方法详解

    asp.net获取系统当前时间的方法详解

    这篇文章主要介绍了asp.net获取系统当前时间的方法,较为详细的分析了C#日期与时间操作所涉及的相关函数与使用技巧,需要的朋友可以参考下
    2016-06-06
  • 使用C#中的Flags特性

    使用C#中的Flags特性

    这篇文章介绍了使用C#中的Flags特性,文中通过示例代码介绍的非常详细。对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2022-07-07
  • C# 计算传入的时间距离今天的时间差

    C# 计算传入的时间距离今天的时间差

    本文通过一段简单的代码给大家介绍了C# 计算传入的时间距离今天的时间差,代码简单易懂,需要的朋友参考下吧
    2017-08-08
  • c#动态调用Webservice的两种方法实例

    c#动态调用Webservice的两种方法实例

    这篇文章介绍了c#动态调用Webservice的两种方法实例,有需要的朋友可以参考一下
    2013-08-08
  • C#中实现PriorityQueue优先级队列的代码

    C#中实现PriorityQueue优先级队列的代码

    这篇文章主要介绍了C#中PriorityQueue优先级队列的实现,构造初始化这部分主要介绍关键的字段和方法,比较器的初始化以及堆的初始化,需要的朋友可以参考下
    2021-12-12
  • 详解Unity安卓共享纹理

    详解Unity安卓共享纹理

    本文主要介绍了Unity安卓共享纹理,对此感兴趣的同学,可以参考下,亲自实验一下,理解其原理。
    2021-05-05
  • c# 抓取Web网页数据分析

    c# 抓取Web网页数据分析

    通过程序自动的读取其它网站网页显示的信息,类似于爬虫程序。比方说我们有一个系统,要提取BaiDu网站上歌曲搜索排名。分析系统在根据得到的数据进行数据分析。为业务提供参考数据。
    2008-11-11
  • 利用windows性能计数器进行服务器性能监控示例分享

    利用windows性能计数器进行服务器性能监控示例分享

    这篇文章主要介绍了利用windows性能计数器进行服务器性能监控的方法,大家可以参考扩展其它功能
    2014-01-01
  • C#逐行读取txt文件的方法

    C#逐行读取txt文件的方法

    这篇文章主要介绍了C#逐行读取txt文件的方法,是C#程序设计中非常实用的一个技巧,需要的朋友可以参考下
    2014-09-09
  • C#中String类常用方法汇总

    C#中String类常用方法汇总

    这篇文章主要介绍了C#中String类常用方法,较为详细的汇总了String类中的常用方法,对于深入掌握C#字符串操作有着很好的学习借鉴价值,需要的朋友可以参考下
    2014-11-11

最新评论