.NET操作XML文件---[读取]
接上一遍博客------(.NET操作XML文件---[添加])
readXml.aspx文件的详情如下:
效果图:
![.NET操作XML资料-[读取]](http://img.reader8.net/uploadfile/jiaocheng/2014015/1442/201401140042307853.jpg)
代码:
<%@ 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"> <div id="div_pizza" runat="server"> </div> </form></body></html>
using System;using System.Collections.Generic;using System.Web;using System.Web.UI;using System.Web.UI.WebControls;using System.Xml;//用于操作xmlpublic 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>"; div_pizza.InnerHtml = htmlString; }}