当前位置: 移动技术网 > IT编程>开发语言>.net > .NET操作XML文件---[读取]

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

2018年02月10日  | 移动技术网IT编程  | 我要评论

if i could fly,广东佛山人才网,flappy bird下载

readXml.aspx文件的详情如下:

 

 

效果图:

 

\

 

 

代码:

 

 

[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;

    }

}

 

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网