.NET操作XML文件---[读取]

接上一遍博客------(.NET操作XML文件---[添加]

readXml.aspx文件的详情如下:

效果图:


代码:

<%@ 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>

readXml.aspx.cs文件的代码如下:
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>";
        div_pizza.InnerHtml = htmlString;
    }
}

<未完待续>


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值