asp.net 4.5 练习~test15-6 xml修改节点

xmlfile1.xml

<?xml version="1.0" encoding="utf-8" ?>
<?xml-stylesheet type="text/xsl" href="style.xsl"?>

<xinwen>
  <news>
    <news_id>1</news_id>
    <news_title>上海国际艺术节开幕</news_title>
    <news_author>王建宁</news_author>
    <news_ly>原创</news_ly>
    <news_content>第十三届中国上海国际艺术节将于11月18日举办。</news_content>
    <news_adddate>2013-10-18</news_adddate>
  </news>
  <news>
    <news_id>2</news_id>
    <news_title>上海首批公租房下月起招租</news_title>
    <news_author>王水宁</news_author>
    <news_ly>转载</news_ly>
    <news_content>两小区分别位于徐汇华庭和上体馆附近。</news_content>
    <news_adddate>2013-10-18</news_adddate>
  </news>
</xinwen>

webform1.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="test15_6.WebForm1" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:GridView ID="GridView1" runat="server" BackColor="White" BorderColor="#999999" BorderStyle="Solid" BorderWidth="1px" CellPadding="3" ForeColor="Black" GridLines="Vertical">
            <AlternatingRowStyle BackColor="#CCCCCC" />
            <FooterStyle BackColor="#CCCCCC" />
            <HeaderStyle BackColor="Black" Font-Bold="True" ForeColor="White" />
            <PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />
            <SelectedRowStyle BackColor="#000099" Font-Bold="True" ForeColor="White" />
            <SortedAscendingCellStyle BackColor="#F1F1F1" />
            <SortedAscendingHeaderStyle BackColor="#808080" />
            <SortedDescendingCellStyle BackColor="#CAC9C9" />
            <SortedDescendingHeaderStyle BackColor="#383838" />
        </asp:GridView>
        请选择节点<asp:DropDownList ID="DropDownList1" runat="server"></asp:DropDownList><br /><br />
        新节点标题名<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        <asp:Button ID="Button1" runat="server" Text="修改" OnClick="Button1_Click" />
    </div>
    </form>
</body>
</html>

webform1.aspx.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml;

namespace test15_6
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                System.Data.DataSet ds = new System.Data.DataSet();
                ds.ReadXml(Server.MapPath("XMLFile1.xml"));
                GridView1.DataSource = ds;
                GridView1.DataBind();
                DropDownList1.DataSource = ds;
                DropDownList1.DataTextField = "news_title";
                DropDownList1.DataBind();
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            XmlDocument doc = new XmlDocument();
            doc.Load(Server.MapPath("XMLFile1.xml"));
            XmlNodeList xnl = doc.SelectSingleNode("xinwen").ChildNodes;
            foreach (XmlNode xn in xnl)
            { 
                XmlElement xe = (XmlElement)xn;
                if (xe.Name == "news")
                {
                    XmlNodeList xnlChild = xe.ChildNodes;
                    foreach (XmlNode xnChild in xnlChild)
                    {
                        XmlElement xeChild = (XmlElement)xnChild;
                        if (xeChild.Name == "news_title" && xeChild.InnerText == DropDownList1.SelectedValue.Trim())
                        {
                            xeChild.InnerText = TextBox1.Text.Trim();
                            Response.Write("<script>alert('修改成功')</script>");
                        }
                    }
                }
            }

            doc.Save(Server.MapPath("XMLFile1.xml"));
            Response.Write("<script>window.location='WebForm1.aspx'</script>");
        }


    }
}

猜你喜欢

转载自blog.csdn.net/modern358/article/details/114837092