.NET操作XML文件---[读取]
效果图:
代码:
[html]
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="readXml.aspx.cs" Inherits="readXml" %>
<!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></title>
</head>
<body>
<form id="form1" runat="server">
<p id="p_pizza" runat="server">
</p>
</form>
</body>
</html>
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="readXml.aspx.cs" Inherits="readXml" %>
<!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></title>
</head>
<body>
<form id="form1" runat="server">
<p id="p_pizza" runat="server">
</p>
</form>
</body>
</html>
readXml.aspx.cs文件的代码如下:[csharp] view plaincopyprint?
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml;//用于操作xml
public partial class readXml : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
ReadPizza();
}
protected void ReadPizza()
{
//创建XML文件对象的实例doc
XmlDocument doc = new XmlDocument();
//加载XML文件
doc.Load(HttpContext.Current.Server.MapPath("XMLFile.xml"));
//获取第一个Pizza结点
XmlNode pizzaNode = doc.DocumentElement.SelectSingleNode("/Pizza");
string htmlString = "";
htmlString += "<table><tr><td>Id</td><td>名称</td><td>尺寸</td><td>价格</td><td>修改操作</td><td>删除操作</td></tr>";
//循环遍历Pizza结点下的子结点
foreach (XmlNode thisNode in pizzaNode)
{
htmlString += "<tr><td>" + thisNode.Attributes["id"].Value + "</td>";
htmlString += "<td>" + thisNode.InnerText + "</td>";
htmlString += "<td>" + thisNode.Attributes["size"].Value + "</td>";
htmlString += "<td>" + thisNode.Attributes["price"].Value + "</td>";
htmlString += "<td><a href='updateXML.aspx?id=" + thisNode.Attributes["id"].Value + "'>修改</a></td>";
htmlString += "<td><a href='deleteXML.aspx?id=" + thisNode.Attributes["id"].Value + "'>删除</a></td></tr>";
}
htmlString += "</table>";
p_pizza.InnerHtml = htmlString;
}
}
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml;//用于操作xml
public partial class readXml : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
ReadPizza();
}
protected void ReadPizza()
{
//创建XML文件对象的实例doc
XmlDocument doc = new XmlDocument();
//加载XML文件
doc.Load(HttpContext.Current.Server.MapPath("XMLFile.xml"));
//获取第一个Pizza结点
XmlNode pizzaNode = doc.DocumentElement.SelectSingleNode("/Pizza");
string htmlString = "";
htmlString += "<table><tr><td>Id</td><td>名称</td><td>尺寸</td><td>价格</td><td>修改操作</td><td>删除操作</td></tr>";
//循环遍历Pizza结点下的子结点
foreach (XmlNode thisNode in pizzaNode)
{
htmlString += "<tr><td>" + thisNode.Attributes["id"].Value + "</td>";
htmlString += "<td>" + thisNode.InnerText + "</td>";
htmlString += "<td>" + thisNode.Attributes["size"].Value + "</td>";
htmlString += "<td>" + thisNode.Attributes["price"].Value + "</td>";
htmlString += "<td><a href='updateXML.aspx?id=" + thisNode.Attributes["id"].Value + "'>修改</a></td>";
htmlString += "<td><a href='deleteXML.aspx?id=" + thisNode.Attributes["id"].Value + "'>删除</a></td></tr>";
}
htmlString += "</table>";
p_pizza.InnerHtml = htmlString;
}
}