下面内容段是关于C# 中xmlreader类的实用演示的内容。
protected void Page_Load(object sender, EventArgs e)
{
int i = 0;
XmlReaderSettings set=new XmlReaderSettings();
set.IgnoreComments=true;
set.IgnoreWhitespace=true;
using (XmlReader reader = XmlReader.Create(Server.MapPath("books.xml"), set)) {
while (reader.Read()) {
if (reader.NodeType == XmlNodeType.Element) {
if (reader.LocalName == "book") {
i++;
}
}
}
Response.Write("<script>alert('共有" + i.ToString() + "本书')</script>");
}
}
xml文档内容
<?xml version="1.0" encoding="utf-8"?>
<books>
<book>
<author>张三</author>
<publisher>清华大学出版社</publisher>
<date>2009-1-1</date>
<name>ASP.NET实训教程</name>
<isbn>978-7-333-20981-4</isbn>
<price>50.00</price>
</book>
<book>
<author>李四</author>
<publisher>机械工业出版社</publisher>
<date>2009-6-8</date>
<name>ASP.NET详解</name>
<isbn>978-7-333-20332-1</isbn>
<price>45.00</price>
</book>
<book>
<author>王五</author>
<publisher>电子工业出版社</publisher>
<date>2010-10-9</date>
<name>C#程序设计</name>
<isbn>978-7-311-21231-2</isbn>
<price>50.00</price>
</book>
<book>
<author>张飞</author>
<publisher>机械工业出版社</publisher>
<date>2008-4-12</date>
<name>.NET设计模式</name>
<isbn>978-7-310-12341-9</isbn>
<price>30.00</price>
</book>
</books>