当前位置:  编程技术>.net/c#/asp.net

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

    来源: 互联网  发布时间:2014-10-18

    本文导语:  1、school.xml,如下: 代码如下:                                                                                                                                     ...

1、school.xml,如下:

代码如下:



 
   
    
        
        
                
                   
                                                 
                
                
                    
                         亢阳
                          女
                    
                    
                         田镇南
                         男
                    
                    
                         刘嘉
                         女
                    
                
        
       
    
       
        
        
                
                   
                                
                
                
                    
                         胡艳
                          女
                    
                    
                         张三
                         男
                    
                    
                         李四
                         女
                    
                
        
       
    
    
   

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,运行,如图:


    
 
 

您可能感兴趣的文章:

 
本站(WWW.)旨在分享和传播互联网科技相关的资讯和技术,将尽最大努力为读者提供更好的信息聚合和浏览方式。
本站(WWW.)站内文章除注明原创外,均为转载、整理或搜集自网络。欢迎任何形式的转载,转载请注明出处。












  • 相关文章推荐
  • C#逐行读取txt文件的方法
  • C# 读取配置文件(指定路径)的方法
  • C# 正则表达式读取Discuz帖子中附件的代码举例
  • c#读取文本的简单例子
  • .net/c#/asp.net iis7站长之家
  • c#简单读取文本的实例方法
  • C# 创建文本文件写入读取实现代码
  • C#读取RSS的小例子
  • c#读取图像保存到数据库(数据库保存图片)实例
  • 解决C# X64应用程序中读取WParam溢出的问题
  • c#多线程读取注册表 c#多线程的小例子
  • c#读取图像保存到数据库中(数据库保存图片)
  • c# 二进制方式读取文本文件的实现代码
  • C#读取EXCEL文件内容写入数据库的代码
  • C#操作图片读取和存储SQLserver实现代码
  • C#读取文件MD5值的实现代码
  • c#读取excel内容内容示例分享
  • C#反射技术(读取和设置类的属性)的例子
  • c# 以二进制读取文本文件
  • C#利用Openxml读取Excel数据实例
  • C++ I/O 成员 getline():读取一行字符
  • read函数读取文件时如何一行一行的读取,请指教!
  • C++ I/O 成员 tellg():使用输入流读取流指针
  • linux 下使用 FREETDS 访问数据库 image 字段的问题, 仅能读取 64K ,超出部分读取不到?
  • C++ I/O 成员 ignore():读取字符并忽略指定字符
  • java中读取.txt和读取.ini文件的方式和命令是否一样
  • C++ I/O 成员 read():读取字条符
  • 上传文件时,如何在读取文件内容之前中断读取,并返回客户端错误信息?
  • C++ I/O 成员 gcount():返回读取的最后一次输入的字符数
  • 光驱中有一张光盘,成功读取,现在想换另一张光盘并读取,该如何做
  • C++ I/O 成员 get():读取字符


  • 站内导航:


    特别声明:169IT网站部分信息来自互联网,如果侵犯您的权利,请及时告知,本站将立即删除!

    ©2012-2021,,E-mail:www_#163.com(请将#改为@)

    浙ICP备11055608号-3