C#中若何應用 XmlReader 讀取XML文件。本站提示廣大學習愛好者:(C#中若何應用 XmlReader 讀取XML文件)文章只能為提供參考,不一定能成為您想要的結果。以下是C#中若何應用 XmlReader 讀取XML文件正文
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類的屬性和辦法:
XmlReader類的主要辦法:
XmlNodeType列舉的成員:
XmlReaderSettings類的主要屬性:
經由過程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文件,這類辦法占用內存少,推舉年夜家應用。