程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#基礎知識 >> C#讀取XML中元素和屬性值的實現代碼

C#讀取XML中元素和屬性值的實現代碼

編輯:C#基礎知識

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,運行,如圖:

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved