• 【XML】-- C#读取XML中元素和属性的值


    Xml是扩展标记语言的简写,是一种开发的文本格式。

          啰嗦几句儿:老师布置的一个小作业却让我的脑细胞死了一堆,难的不是代码,是n多嵌套的if、foreach,做完这个,我使劲儿想:我一女孩,没有更多女孩的生活整天对着电脑学,累的不行但这水平就不见长,哪儿出错了呢?----脑袋瓜子不够用。。。  哎,来句老师的话缓解一下:“JUST DO IT!很多孩子听了乔丹的话,从此进入IT行业~~~”

          over,我还是把这个简单但却打击了我下的小东西共享下吧。^_~

          用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("	");
     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("
    按随意键跳出");
    113                             Console.ReadKey();
    114                         }
    115 
    116                     }
    117                 }
    118             }
    119         }
    120     }
    复制代码

       ok,运行,如图:

              

        -- 后记,学的xml不多,做完之后,我不得不对自己说:IT,要学的太多了,...>_<...

  • 相关阅读:
    Windows Server2012R2 添加Microsoft .NET Framework 3.5 功能失败的解决方法
    Windows Server2012R2 安装 SharePoint 2013 的必备组件
    pig加载两个不同字段个数的文件?load file with different items(f1有42列,f2有43列读到一个对象中)
    正则表达式的子模式详解
    PHP 递归函数的理解
    仿照美食杰tab选项卡
    tab简单选项卡
    PHP字符串变驼峰方法
    PHP笔试题
    PHP面试题集
  • 原文地址:https://www.cnblogs.com/dxqNet/p/7212479.html
Copyright © 2020-2023  润新知