XmlReader通過向前讀取文檔并識別讀取到的元素,為我們提供了一種消耗資源最少的方式來解析XML數據。很多時候我們都是利用XmlReader來對XML文件的數據有效性進行驗證(使用XmlReader實例的Read()方法依次讀取所有節點,以此判斷是否與符合指定的模式)。使用這種非緩存、只讀、只向前的方式,每次讀取只將很少的數據放入內存,對內存的占用量較小,對于讀取內容較大的XML文件不失為一種最佳的選擇。
讓我們看看XmlReader類讀取XML文件的步驟:
1、使用XmlReader類的Create()工廠方法創建該類的一個實例,并將被讀取的XML文件名作為參數傳入方法;
2、建立一個反復調用Read()方法的循環。這個方法從文件的第一個節點開始,然后讀取所有余下的節點,但每次調用只讀取一個節點。如果存在一個節點可被讀取則返回True,而當到達文件最后時則返回False;
3、在這個循環中,將檢查XmlReader實例的屬性和方法,以獲得關于當前節點的信息(節點的類型、名稱、數據等)。不斷執行循環直到Read()返回False;
下面首先看一個示例:
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>
輸出結果:
=> employees
====> employee
=======> name
==========> firstName
==========> lastName
=======> city
=======> state
=======> zipCode
====> employee
=======> name
==========> firstName
==========> lastName
=======> city
=======> state
=======> zipCode
下面讓我們看看XmlReader類的屬性和方法:
屬 性 | 說 明 |
---|---|
AttributeCount | 返回當前節點的屬性個數 |
Depth | 返回當前節點的深度,用于判斷指定的節點是否具有子節點 |
EOF | 判斷讀取器是否位于流的末端 |
HasAttribute | 返回指示當前節點是否具有屬性的布爾值 |
HasValue | 返回指示當前節點是否具有值的布爾值 |
IsEmptyElement | 判斷當前節點是否是一個空元素 |
LocalName | 返回當前節點的本地名稱 |
Name | 返回當前節點的限定名稱 |
NamespaceURI | 返回當前節點的命名空間URI |
NodeType | 以XmlNodeType枚舉的形式返回當前節點的節點類型 |
Prefix | 返回與當前節點相關的命名空間前綴 |
ReadState | 以ReadState枚舉的形式返回讀取器的當前狀態 |
Settings | 返回用于創建XmlReader實例的XmlReaderSettings對象 |
Value | 返回當前節點的值 |
ValueType | 獲得當前節點的CLR類型 |
XmlReader類的重要方法:
方 法 | 說 明 |
---|---|
Close | 通過將ReadState枚舉設置為Closed來關閉Xmlreader對象 |
Create | 創建XmlReader對象的實例并將其返回給調用程序 |
GetAttribute | 獲得屬性的值 |
IsStartElement | 指示當前節點是否是開始標簽 |
MoveToAttribute | 移動讀取器到指定的屬性 |
MoveToContent | 如果當前節點不是內容節點,則移動讀取器至下一個內容節點 |
MoveToElement | 移動讀取器至包含當前屬性的元素;用于列舉屬性以及想切換至包含所有這些屬性的元素 |
MoveToFirstAttribute | 移動讀取器至當前節點的第一個屬性 |
MoveToNextAttribute | 移動讀取器至當前節點的下一個屬性 |
Read | 從流中讀取下一個節點 |
ReadContentAs | 讀取提供類型的對象的內容 |
ReadElementContentAs | 讀取當前元素并返回指定類型對象的內容 |
ReadEndElement | 移動讀取器越過當前結束標簽并移動到下一個節點 |
ReadInnerXml | 以字符串的形式讀取包括標記在內的當前節點所有內容 |
ReadOutXml | 讀取包括當前節點標記和子節點在內的節點的內容 |
ReadToDescendant | 移動讀取器至下一個匹配子孫元素的節點 |
ReadToFollowing | 不斷讀取直至找到指定的元素 |
ReadToNextSlibing | 移動讀取器至下一個匹配兄弟元素的節點 |
ReadValueChunk | 允許讀取嵌入在XML文檔中的大型文本流 |
XmlNodeType枚舉的成員:
成 員 | 說 明 |
---|---|
Attribute | 屬性 |
CDATA | CDATA區域 |
Comment | XML注釋 |
Document | 文檔對象,表示XML樹的根 |
DocumentFragment | 文檔片斷 |
DocumentType | 文檔類型聲明 |
Element,EndElement | 開始元素和結束元素 |
Entity,EndEntity | 開始實體聲明和結束實體聲明 |
EntityReference | 實體引用(如<) |
None | 有沒有讀取節點而查詢節點類型時使用 |
Notation | DTD中的符號條目 |
ProcessingInstruction | XML處理指令 |
SignificantWhitespace | 在混合內容模型文檔中的空白,或者設置了xml:space=preserve時使用 |
Text | 元素的文本內容 |
Whitespace | 標記之間的空白 |
XmlDeclaration | 在文檔頂部的XML聲明 |
XmlReaderSettings類的重要屬性:
屬 性 | 說 明 |
---|---|
CheckCharacters | 允許你獲得或者設置用于指示是否執行字符檢查的值 |
ConformanceLevel | 獲得或設置XmlReader對象的符合要求 |
IgnoreComment | 允許你獲得或設置用于指示是否忽略注釋的值 |
IgnoreProcessingInstruct | 指定是否忽略處理指令 |
IgnoreWhitespace | 指定是否忽略無意義的空格 |
ProhibitDtd | 指定是否允許DTD處理 |
Schemas | 指定在執行XML驗證時使用的XmlSchemaSet |
ValidationFlags | 獲得或者設置用于指定模式驗證設置的值 |
ValidationType | 獲得或者設置用于指定所執行的驗證類型的值 |
XmlResolver | 設置用于訪問外部文檔的XmlReslover |
通過XmlReaderSettings類,你可以指定一系列由XmlReader對象支持的功能,為此,只需將XmlReaderSettings作為參數傳入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>
總結下來,我們可以使用XmlReader類以非緩存、只讀、只向前的方式讀取XML文件,這種方法占用內存少,推薦大家使用。
新聞熱點
疑難解答