<?xml version="1.0" encoding="utf-8" ?>
<!— This is list of employees —>
<employees>
<employee employeeid="1">
<firstname>Nancy</firstname>
<lastname>Davolio</lastname>
<homephone>(206) 555-9857</homephone>
<notes>
<![CDATA[includes a BA in psychology from Colorado State University in
1970. She also completed "The Art of the Cold Call." Nancy is a member of
Toastmasters International.]]>
</notes>
</employee>
<employee employeeid="2">
<firstname>Andrew</firstname>
<lastname>Fuller</lastname>
<homephone>(206) 555-9482</homephone>
<notes>
<![CDATA[Andrew received his BTS commercial in 1974 and a Ph.D. in
international marketing from the University of Dallas in 1981. He is fluent
in French and Italian and reads German. He joined the company as a sales
representative, was promoted to sales manager in January 1992 and to vice
president of sales in March 1993. Andrew is a member of the Sales
Management Roundtable, the Seattle Chamber of Commerce, and the Pacific
Rim Importers Association.]]>
</notes>
</employee>
<employee employeeid="3">
<firstname>Janet</firstname>
<lastname>Leverling</lastname>
<homephone>(206) 555-3412</homephone>
<notes>
<![CDATA[Janet has a BS degree in chemistry from Boston College (1984).
She has also completed a certificate program in food retailing management.
Janet was hired as a sales associate in 1991 and promoted to sales
representative in February 1992.]]>
</notes>
</employee>
</employees>
ReadSubTree()方法:读取当前节点的子节点并返回XMLReader实例的一个子树。当你解析一个较大的XML文件,但是只希望处理其中的少部分数据的时候,这个方法非常有效。
(一)XML Readers和Writers类
(二)什么时候使用XmlReader和XmlWriter类
XmlReader类
? You need to only read the document.
? The document is huge.
? You need to keep the memory footprint small.
? You want to work with many XML documents that are a reasonable size.
? You do not want to access various parts of the document randomly.
XmlWriter类
? You want to only write content.
? You want to keep the memory footprint small.
? You are writing huge XML documents and looking for better performance.
(三)Reader类
使用XmlTextReader类打开XML文档:
1: using System;
2: using System.Xml;
3: using System.IO;
4: using System.Text;
5:
6: public partial class Default7 : System.Web.UI.Page
7: {
8: protected void Page_Load(object sender, EventArgs e)
9: {
10:
11: }
12: protected void Button1_Click(object sender, EventArgs e)
13: {
14: XmlTextReader reader;
15: if (RadioButton1.Checked)
16: {
17: reader = new XmlTextReader(TextBox1.Text);
18: }
19: if (RadioButton2.Checked)
20: {
21: FileStream stream = File.OpenRead(TextBox1.Text);
22: reader = new XmlTextReader(stream);
23: stream.Close();
24: reader.Close();
25: }
26: if (RadioButton3.Checked)
27: {
28: MemoryStream ms = new MemoryStream();
29: byte[] data = ASCIIEncoding.ASCII.GetBytes(TextBox1.Text);
30: ms.Write(data, 0, data.Length);
31: reader = new XmlTextReader(ms);
32: //some processing code
33: ms.Close();
34: reader.Close();
35: }
36: Response.Write("XML Document Opened Successfully!");
37: }
38: }
- XmlValidatingReader类
- XmlNodeReader类