XmlTextReader 类
表示提供对 XML 数据进行快速、非缓存、只进访问的读取器。
1
2 1using System;
3 2using System.Xml;
4 3
5 4public partial class ReadXml : System.Web.UI.Page
6 5{
7 6 protected void Read_Click(object sender, EventArgs e)
8 7 {
9 8 XmlTextReader xmlReader = null;
10 9 //可以阻止阅读器返回空白节点
1110 try
1211 {
1312 xmlReader = new XmlTextReader(Server.MapPath("First.xml"));
1413 xmlReader.WhitespaceHandling = WhitespaceHandling.None;
1514
1615 while (xmlReader.Read())
1716 {
1817 Response.Write(xmlReader.NodeType.ToString() + " " + xmlReader.Name.ToString() + " " + xmlReader.Value.ToString()+"<br>");
1918 if (xmlReader.AttributeCount > 0)
2019 {
2120 while (xmlReader.MoveToNextAttribute())
2221 {
2322 Response.Write(xmlReader.NodeType.ToString() + " " + xmlReader.Name.ToString() + " " + xmlReader.Value.ToString()+"<br>");
2423 }
2524 }
2625 }
2726 }
2827 finally
2928 {
3029 if (xmlReader != null)
3130 {
3231 xmlReader.Close();
3332 }
3433 }
3534 }
3635}
3736
2 1using System;
3 2using System.Xml;
4 3
5 4public partial class ReadXml : System.Web.UI.Page
6 5{
7 6 protected void Read_Click(object sender, EventArgs e)
8 7 {
9 8 XmlTextReader xmlReader = null;
10 9 //可以阻止阅读器返回空白节点
1110 try
1211 {
1312 xmlReader = new XmlTextReader(Server.MapPath("First.xml"));
1413 xmlReader.WhitespaceHandling = WhitespaceHandling.None;
1514
1615 while (xmlReader.Read())
1716 {
1817 Response.Write(xmlReader.NodeType.ToString() + " " + xmlReader.Name.ToString() + " " + xmlReader.Value.ToString()+"<br>");
1918 if (xmlReader.AttributeCount > 0)
2019 {
2120 while (xmlReader.MoveToNextAttribute())
2221 {
2322 Response.Write(xmlReader.NodeType.ToString() + " " + xmlReader.Name.ToString() + " " + xmlReader.Value.ToString()+"<br>");
2423 }
2524 }
2625 }
2726 }
2827 finally
2928 {
3029 if (xmlReader != null)
3130 {
3231 xmlReader.Close();
3332 }
3433 }
3534 }
3635}
3736
原Xml文档:
1<?xml version="1.0" encoding="utf-8"?>
2<Teachers>
3 <Teacher number="firstone">
4 <name>John</name>
5 <Nianling>12</Nianling>
6 <sex>woman</sex>
7 <xueli ninn="11212" xmlns="gao" />
8 <xueke>shuxue</xueke>
9 </Teacher>
10</Teachers>
2<Teachers>
3 <Teacher number="firstone">
4 <name>John</name>
5 <Nianling>12</Nianling>
6 <sex>woman</sex>
7 <xueli ninn="11212" xmlns="gao" />
8 <xueke>shuxue</xueke>
9 </Teacher>
10</Teachers>
输出结果:
1Attribute version 1.0
2Attribute encoding utf-8
3Element Teachers
4Element Teacher
5Attribute number firstone
6Element name
7Text John
8EndElement name
9Element Nianling
10Text 12
11EndElement Nianling
12Element sex
13Text woman
14EndElement sex
15Element xueli
16Attribute ninn 11212
17Attribute xmlns gao
18Element xueke
19Text shuxue
20EndElement xueke
21EndElement Teacher
22EndElement Teachers
2Attribute encoding utf-8
3Element Teachers
4Element Teacher
5Attribute number firstone
6Element name
7Text John
8EndElement name
9Element Nianling
10Text 12
11EndElement Nianling
12Element sex
13Text woman
14EndElement sex
15Element xueli
16Attribute ninn 11212
17Attribute xmlns gao
18Element xueke
19Text shuxue
20EndElement xueke
21EndElement Teacher
22EndElement Teachers