XML参考 :XmlReader 详解、实例(4)-- 读取XML内容
.NET Framework 类库
XmlReader 类
表示提供对 XML 数据进行快速、非缓存、只进访问的读取器,即 对 XML 数据流的只进只读访问。XmlReader 类符合 W3C 可扩展标记语言 (XML) 1.0 和“XML 中的命名空间”建议。
1.实例:读取XML内容
page页面代码:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication1._Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div style="width:400px; border:solid 1px #000; background-color:#8ABBDF; color:White;">
<asp:Label ID="mEmployeesLabel" runat="server" Text=""></asp:Label>
</div>
</form>
</body>
</html>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div style="width:400px; border:solid 1px #000; background-color:#8ABBDF; color:White;">
<asp:Label ID="mEmployeesLabel" runat="server" Text=""></asp:Label>
</div>
</form>
</body>
</html>
2.读取XML内容的代码
protected void Page_Load(object sender, EventArgs e)
{
string employeeID = "";
string xmlFilePath = Request.PhysicalApplicationPath + @"\Employees.xml";
try
{
using (XmlReader reader = XmlReader.Create(xmlFilePath))
{
this.mEmployeesLabel.Text = "<b>Employees</b>";
this.mEmployeesLabel.Text += "<ul>";
while (reader.Read())
{
if (reader.NodeType == XmlNodeType.Element)
{
if (reader.Name == "employee")
{
employeeID = reader.GetAttribute("id");
}
if (reader.Name == "name")
{
this.mEmployeesLabel.Text += "<li>" + "Employee-" + employeeID;
this.mEmployeesLabel.Text += "<ul>";
this.mEmployeesLabel.Text += "<li>ID-" + employeeID + "</li>";
}
if (reader.Name == "firstName")
{
this.mEmployeesLabel.Text += "<li>First Name-" + reader.ReadString() + "</li>";
}
if (reader.Name == "lastName")
{
this.mEmployeesLabel.Text += "<li>Last Name-" + reader.ReadString() + "</li>";
}
if (reader.Name == "city")
{
this.mEmployeesLabel.Text += "<li>City-" + reader.ReadString() + "</li>";
}
if (reader.Name == "state")
{
this.mEmployeesLabel.Text += "<li>state-" + reader.ReadString() + "</li>";
}
if (reader.Name == "zipCode")
{
this.mEmployeesLabel.Text += "<li>state-" + reader.ReadElementContentAsInt() + "</li>";
}
}
else if (reader.NodeType == XmlNodeType.EndElement)
{
if (reader.Name == "employee")
{
this.mEmployeesLabel.Text += "</ul>";
this.mEmployeesLabel.Text += "</li>";
}
}
}
this.mEmployeesLabel.Text += "</ul>";
}
}
catch (Exception ex)
{
this.mEmployeesLabel.Text = "An Exception occured:" + ex.Message;
}
}
{
string employeeID = "";
string xmlFilePath = Request.PhysicalApplicationPath + @"\Employees.xml";
try
{
using (XmlReader reader = XmlReader.Create(xmlFilePath))
{
this.mEmployeesLabel.Text = "<b>Employees</b>";
this.mEmployeesLabel.Text += "<ul>";
while (reader.Read())
{
if (reader.NodeType == XmlNodeType.Element)
{
if (reader.Name == "employee")
{
employeeID = reader.GetAttribute("id");
}
if (reader.Name == "name")
{
this.mEmployeesLabel.Text += "<li>" + "Employee-" + employeeID;
this.mEmployeesLabel.Text += "<ul>";
this.mEmployeesLabel.Text += "<li>ID-" + employeeID + "</li>";
}
if (reader.Name == "firstName")
{
this.mEmployeesLabel.Text += "<li>First Name-" + reader.ReadString() + "</li>";
}
if (reader.Name == "lastName")
{
this.mEmployeesLabel.Text += "<li>Last Name-" + reader.ReadString() + "</li>";
}
if (reader.Name == "city")
{
this.mEmployeesLabel.Text += "<li>City-" + reader.ReadString() + "</li>";
}
if (reader.Name == "state")
{
this.mEmployeesLabel.Text += "<li>state-" + reader.ReadString() + "</li>";
}
if (reader.Name == "zipCode")
{
this.mEmployeesLabel.Text += "<li>state-" + reader.ReadElementContentAsInt() + "</li>";
}
}
else if (reader.NodeType == XmlNodeType.EndElement)
{
if (reader.Name == "employee")
{
this.mEmployeesLabel.Text += "</ul>";
this.mEmployeesLabel.Text += "</li>";
}
}
}
this.mEmployeesLabel.Text += "</ul>";
}
}
catch (Exception ex)
{
this.mEmployeesLabel.Text = "An Exception occured:" + ex.Message;
}
}
3.效果如下: