C#中如何使用 XmlReader 讀取XML文件
XmlReader通過(guò)向前讀取文檔并識(shí)別讀取到的元素,為我們提供了一種消耗資源最少的方式來(lái)解析XML數(shù)據(jù)。很多時(shí)候我們都是利用XmlReader來(lái)對(duì)XML文件的數(shù)據(jù)有效性進(jìn)行驗(yàn)證(使用XmlReader實(shí)例的Read()方法依次讀取所有節(jié)點(diǎn),以此判斷是否與符合指定的模式)。使用這種非緩存、只讀、只向前的方式,每次讀取只將很少的數(shù)據(jù)放入內(nèi)存,對(duì)內(nèi)存的占用量較小,對(duì)于讀取內(nèi)容較大的XML文件不失為一種最佳的選擇。
讓我們看看XmlReader類(lèi)讀取XML文件的步驟:
1、使用XmlReader類(lèi)的Create()工廠方法創(chuàng)建該類(lèi)的一個(gè)實(shí)例,并將被讀取的XML文件名作為參數(shù)傳入方法;
2、建立一個(gè)反復(fù)調(diào)用Read()方法的循環(huán)。這個(gè)方法從文件的第一個(gè)節(jié)點(diǎn)開(kāi)始,然后讀取所有余下的節(jié)點(diǎn),但每次調(diào)用只讀取一個(gè)節(jié)點(diǎn)。如果存在一個(gè)節(jié)點(diǎn)可被讀取則返回True,而當(dāng)?shù)竭_(dá)文件最后時(shí)則返回False;
3、在這個(gè)循環(huán)中,將檢查XmlReader實(shí)例的屬性和方法,以獲得關(guān)于當(dāng)前節(jié)點(diǎn)的信息(節(jié)點(diǎn)的類(lèi)型、名稱(chēng)、數(shù)據(jù)等)。不斷執(zhí)行循環(huán)直到Read()返回False;
下面首先看一個(gè)示例:
Employees.xml文件:
<?xml version='1.0'?> <employees> <employee id="1"> <name> <firstName>Nancy</firstName> <lastName>Davolio</lastName> </name> <city>Seattle</city> <state>WA</state> <zipCode>98122</zipCode> </employee> <employee id="2"> <name> <firstName>Andrew</firstName> <lastName>Fuller</lastName> </name> <city>Tacoma</city> <state>WA</state> <zipCode>98401</zipCode> </employee> </employees>
aspx代碼:
<%@ Page Language="C#" %> <%@ Import Namespace="System.Xml" %> <script runat="server"> void Page_Load(object sender, EventArgs e) { //Location of XML file string xmlFilePath = Server.MapPath("~/Employees.xml"); try { using (XmlReader reader = XmlReader.Create(xmlFilePath)) { string result; while (reader.Read()) { //Process only the elements if (reader.NodeType == XmlNodeType.Element) { result = ""; for (int count = 1; count <= reader.Depth; count++) { result += "==="; } result += "=> " + reader.Name + "<br/>"; lblResult.Text += result; } } } } catch (Exception ex) { lblResult.Text = "An Exception occurred: " + ex.Message; } } </script> <html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server"> <title>Reading an XML File using XmlReader</title> </head> <body> <form id="form1" runat="server"> <div> <asp:label id="lblResult" runat="server" /> </div> </form> </body> </html>
輸出結(jié)果:
=> employees
====> employee
=======> name
==========> firstName
==========> lastName
=======> city
=======> state
=======> zipCode
====> employee
=======> name
==========> firstName
==========> lastName
=======> city
=======> state
=======> zipCode
下面讓我們看看XmlReader類(lèi)的屬性和方法:
屬 性 | 說(shuō) 明 |
---|---|
AttributeCount | 返回當(dāng)前節(jié)點(diǎn)的屬性個(gè)數(shù) |
Depth | 返回當(dāng)前節(jié)點(diǎn)的深度,用于判斷指定的節(jié)點(diǎn)是否具有子節(jié)點(diǎn) |
EOF | 判斷讀取器是否位于流的末端 |
HasAttribute | 返回指示當(dāng)前節(jié)點(diǎn)是否具有屬性的布爾值 |
HasValue | 返回指示當(dāng)前節(jié)點(diǎn)是否具有值的布爾值 |
IsEmptyElement | 判斷當(dāng)前節(jié)點(diǎn)是否是一個(gè)空元素 |
LocalName | 返回當(dāng)前節(jié)點(diǎn)的本地名稱(chēng) |
Name | 返回當(dāng)前節(jié)點(diǎn)的限定名稱(chēng) |
NamespaceURI | 返回當(dāng)前節(jié)點(diǎn)的命名空間URI |
NodeType | 以XmlNodeType枚舉的形式返回當(dāng)前節(jié)點(diǎn)的節(jié)點(diǎn)類(lèi)型 |
Prefix | 返回與當(dāng)前節(jié)點(diǎn)相關(guān)的命名空間前綴 |
ReadState | 以ReadState枚舉的形式返回讀取器的當(dāng)前狀態(tài) |
Settings | 返回用于創(chuàng)建XmlReader實(shí)例的XmlReaderSettings對(duì)象 |
Value | 返回當(dāng)前節(jié)點(diǎn)的值 |
ValueType | 獲得當(dāng)前節(jié)點(diǎn)的CLR類(lèi)型 |
XmlReader類(lèi)的重要方法:
方 法 | 說(shuō) 明 |
---|---|
Close | 通過(guò)將ReadState枚舉設(shè)置為Closed來(lái)關(guān)閉Xmlreader對(duì)象 |
Create | 創(chuàng)建XmlReader對(duì)象的實(shí)例并將其返回給調(diào)用程序 |
GetAttribute | 獲得屬性的值 |
IsStartElement | 指示當(dāng)前節(jié)點(diǎn)是否是開(kāi)始標(biāo)簽 |
MoveToAttribute | 移動(dòng)讀取器到指定的屬性 |
MoveToContent | 如果當(dāng)前節(jié)點(diǎn)不是內(nèi)容節(jié)點(diǎn),則移動(dòng)讀取器至下一個(gè)內(nèi)容節(jié)點(diǎn) |
MoveToElement | 移動(dòng)讀取器至包含當(dāng)前屬性的元素;用于列舉屬性以及想切換至包含所有這些屬性的元素 |
MoveToFirstAttribute | 移動(dòng)讀取器至當(dāng)前節(jié)點(diǎn)的第一個(gè)屬性 |
MoveToNextAttribute | 移動(dòng)讀取器至當(dāng)前節(jié)點(diǎn)的下一個(gè)屬性 |
Read | 從流中讀取下一個(gè)節(jié)點(diǎn) |
ReadContentAs | 讀取提供類(lèi)型的對(duì)象的內(nèi)容 |
ReadElementContentAs | 讀取當(dāng)前元素并返回指定類(lèi)型對(duì)象的內(nèi)容 |
ReadEndElement | 移動(dòng)讀取器越過(guò)當(dāng)前結(jié)束標(biāo)簽并移動(dòng)到下一個(gè)節(jié)點(diǎn) |
ReadInnerXml | 以字符串的形式讀取包括標(biāo)記在內(nèi)的當(dāng)前節(jié)點(diǎn)所有內(nèi)容 |
ReadOutXml | 讀取包括當(dāng)前節(jié)點(diǎn)標(biāo)記和子節(jié)點(diǎn)在內(nèi)的節(jié)點(diǎn)的內(nèi)容 |
ReadToDescendant | 移動(dòng)讀取器至下一個(gè)匹配子孫元素的節(jié)點(diǎn) |
ReadToFollowing | 不斷讀取直至找到指定的元素 |
ReadToNextSlibing | 移動(dòng)讀取器至下一個(gè)匹配兄弟元素的節(jié)點(diǎn) |
ReadValueChunk | 允許讀取嵌入在XML文檔中的大型文本流 |
XmlNodeType枚舉的成員:
成 員 | 說(shuō) 明 |
---|---|
Attribute | 屬性 |
CDATA | CDATA區(qū)域 |
Comment | XML注釋 |
Document | 文檔對(duì)象,表示XML樹(shù)的根 |
DocumentFragment | 文檔片斷 |
DocumentType | 文檔類(lèi)型聲明 |
Element,EndElement | 開(kāi)始元素和結(jié)束元素 |
Entity,EndEntity | 開(kāi)始實(shí)體聲明和結(jié)束實(shí)體聲明 |
EntityReference | 實(shí)體引用(如<) |
None | 有沒(méi)有讀取節(jié)點(diǎn)而查詢(xún)節(jié)點(diǎn)類(lèi)型時(shí)使用 |
Notation | DTD中的符號(hào)條目 |
ProcessingInstruction | XML處理指令 |
SignificantWhitespace | 在混合內(nèi)容模型文檔中的空白,或者設(shè)置了xml:space=preserve時(shí)使用 |
Text | 元素的文本內(nèi)容 |
Whitespace | 標(biāo)記之間的空白 |
XmlDeclaration | 在文檔頂部的XML聲明 |
XmlReaderSettings類(lèi)的重要屬性:
屬 性 | 說(shuō) 明 |
---|---|
CheckCharacters | 允許你獲得或者設(shè)置用于指示是否執(zhí)行字符檢查的值 |
ConformanceLevel | 獲得或設(shè)置XmlReader對(duì)象的符合要求 |
IgnoreComment | 允許你獲得或設(shè)置用于指示是否忽略注釋的值 |
IgnoreProcessingInstruct |
指定是否忽略處理指令 |
IgnoreWhitespace | 指定是否忽略無(wú)意義的空格 |
ProhibitDtd | 指定是否允許DTD處理 |
Schemas | 指定在執(zhí)行XML驗(yàn)證時(shí)使用的XmlSchemaSet |
ValidationFlags | 獲得或者設(shè)置用于指定模式驗(yàn)證設(shè)置的值 |
ValidationType | 獲得或者設(shè)置用于指定所執(zhí)行的驗(yàn)證類(lèi)型的值 |
XmlResolver | 設(shè)置用于訪問(wèn)外部文檔的XmlReslover |
通過(guò)XmlReaderSettings類(lèi),你可以指定一系列由XmlReader對(duì)象支持的功能,為此,只需將XmlReaderSettings作為參數(shù)傳入XmlReader的Create()方法中即可。如下所示:
<script runat="server"> void Page_Load(object sender, EventArgs e) { string xmlFilePath = Server.Mappath("~/Employees.xml"); //Create the XmlReaderSettings object and set appropriate properties XmlReaderSettings settings = new XmlReaderSettings(); settings.IgnoreComments = true; settings.IgnoreWhitespace = true; try { //Get reference to the XmlReader object using (XmlReader reader = XmlReader.Create(xmlFilePath, settings)) { string result; while (reader.Read()) { //Process only the elements if (reader.NodeType == XmlNodeType.Element) { //Reset the variable for a new element result = ""; for (int count = 1; count <= reader.Depth; count++) { result += "==="; } result += "=> " + reader.Name + "<br/>"; lblResult.Text += result; } } } } catch (Exception ex) { lblResult.Text = "An Exception occurred: " + ex.Message; } } </script>
總結(jié)下來(lái),我們可以使用XmlReader類(lèi)以非緩存、只讀、只向前的方式讀取XML文件,這種方法占用內(nèi)存少,推薦大家使用。
- C#讀取XML的CDATA節(jié)點(diǎn)內(nèi)容實(shí)例詳解
- C#實(shí)現(xiàn)XML文件讀取
- c#讀取XML多級(jí)子節(jié)點(diǎn)
- C# 創(chuàng)建,讀取,寫(xiě)入XML文件
- C#讀取XML的三種實(shí)現(xiàn)方式
- 詳解c#讀取XML的實(shí)例代碼
- C# winfrom實(shí)現(xiàn)讀取修改xml
- 詳解C#借助.NET框架中的XmlTextReader類(lèi)讀取XML的方法
- C#保存與讀取DataTable信息到XML格式的方法
- C#讀取xml節(jié)點(diǎn)數(shù)據(jù)方法小結(jié)
- C#從文件流讀取xml文件到DataSet并顯示的方法
- C#讀取Excel并轉(zhuǎn)化成XML的方法
- c# 讀取XML文件的示例
相關(guān)文章
C#使用迭代法實(shí)現(xiàn)Fibnaci數(shù)列
這篇文章主要介紹了C#使用迭代法實(shí)現(xiàn)Fibnaci數(shù)列的方法,較為詳細(xì)的分析了Fibnaci數(shù)列的原理與迭代法實(shí)現(xiàn)技巧,需要的朋友可以參考下2015-05-05C#如何在窗體程序中操作數(shù)據(jù)庫(kù)數(shù)據(jù)
這篇文章主要介紹了C#如何在窗體程序中操作數(shù)據(jù)庫(kù)數(shù)據(jù),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-04-04C# TreeView讀取數(shù)據(jù)庫(kù)簡(jiǎn)單實(shí)例
這篇文章主要介紹了2013-12-12