XML做的留言板

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

<!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>
    <style type="text/css">
        .table
        {
            font-size: 10pt;
            color: #666666;
      
        }
    </style>
</head>
<body style=" margin-top:0px;">
    <form id="form1" runat="server">
    <div align="center">
        <asp:GridView ID="gridContent" runat="server" BackColor="White" BorderColor="#CC9966"
            BorderStyle="None" BorderWidth="1px" CellPadding="4" Width="315px">
            <FooterStyle BackColor="#FFFFCC" ForeColor="#330099" />
            <HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="#FFFFCC" />
            <PagerStyle BackColor="#FFFFCC" ForeColor="#330099" HorizontalAlign="Center" />
            <RowStyle BackColor="White" ForeColor="#330099" />
            <SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="#663399" />
            <SortedAscendingCellStyle BackColor="#FEFCEB" />
            <SortedAscendingHeaderStyle BackColor="#AF0101" />
            <SortedDescendingCellStyle BackColor="#F6F0C0" />
            <SortedDescendingHeaderStyle BackColor="#7E0000" />
        </asp:GridView>
    </div>
    <div align="center">
        <table class="table">
            <tr>
                <td>
                    留言人:
                </td>
                <td class="style1">
                    <asp:TextBox ID="txtName" runat="server" Width="243px"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td>
                    QQ:
                </td>
                <td class="style1">
                    <asp:TextBox ID="txtQQ" runat="server" Width="243px"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td>
                    Email:
                </td>
                <td class="style1">
                    <asp:TextBox ID="txtEmail" runat="server" Width="243px"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td>
                    Tel:
                </td>
                <td class="style1">
                    <asp:TextBox ID="txtTel" runat="server" Width="243px"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td>
                    留言内容:
                </td>
                <td class="style1">
                    <asp:TextBox ID="txtCotent" runat="server" Height="143px" TextMode="MultiLine" Width="243px"></asp:TextBox>
                </td>
            </tr>
            <tr align="center">
                <td colspan="2">
                    <asp:Button ID="btnSubmit" runat="server" Text="提交" OnClick="btnSubmit_Click" />
                    <input id="btnReset" type="reset" value="重置" />
                </td>
            </tr>
        </table>
    </div>
    <div class="table" align="center">
        输入你要查找的留言人:<asp:TextBox ID="txtSelect" runat="server"></asp:TextBox>
        <asp:Button ID="btnSelect" runat="server" Text="搜索" Width="61px"
            onclick="btnSelect_Click" />
    </div>
    <div align="center">
        <table class="table">
            <tr>
                <td>
                    留言人:
                </td>
                <td>
                    <asp:Label ID="lblPerson" runat="server" Text="Label"></asp:Label>
                </td>
            </tr>
            <tr>
                <td>
                    QQ:
                </td>
                <td>
                    <asp:Label ID="lblQQ" runat="server" Text="Label"></asp:Label>
                </td>
            </tr>
            <tr>
                <td>
                    Email:
                </td>
                <td>
                    <asp:Label ID="lblEmail" runat="server" Text="Label"></asp:Label>
                </td>
            </tr>
            <tr>
                <td>
                    Tel:
                </td>
                <td>
                    <asp:Label ID="lblTel" runat="server" Text="Label"></asp:Label>
                </td>
            </tr>
            <tr>
                <td>
                    Content:
                </td>
                <td>
                    <asp:Label ID="lblContent" runat="server" Text="Label"></asp:Label>
                </td>
            </tr>
        </table>
    </div>
    </form>
</body>
</html>

-------------------------------------------------------------------------------------------------------------

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

namespace XMLLeaveWordBorad
{
    public partial class Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindGrid();
            }
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            XmlDocument doc = new XmlDocument();
            doc.Load(Server.MapPath("Content.xml"));
            XmlNode xnode = doc.SelectSingleNode("content");
            XmlElement el = doc.CreateElement("person");
            XmlAttribute attrName = doc.CreateAttribute("name");
            attrName.Value = txtName.Text;
            el.Attributes.Append(attrName);
            XmlAttribute attrQQ = doc.CreateAttribute("qq");
            attrQQ.Value = txtQQ.Text;
            el.Attributes.Append(attrQQ);
            XmlAttribute attrEmail = doc.CreateAttribute("email");
            attrEmail.Value = txtEmail.Text;
            el.Attributes.Append(attrEmail);
            XmlAttribute attrTel = doc.CreateAttribute("tel");
            attrTel.Value = txtTel.Text;
            el.Attributes.Append(attrTel);
            el.InnerText = txtCotent.Text;
            xnode.AppendChild(el);
            doc.Save(Server.MapPath("Content.xml"));
            BindGrid();
        }

        public void BindGrid()
        {
            DataSet store = new DataSet();
            store.ReadXml(Server.MapPath("Content.xml"));
            gridContent.DataSource = store;
            gridContent.DataBind();
        }

        protected void btnSelect_Click(object sender, EventArgs e)
        {
            XmlDocument doc = new XmlDocument();
            doc.Load(Server.MapPath("Content.xml"));
            XmlNode xnode = doc.SelectSingleNode("content");
            XmlNodeList list = doc.SelectSingleNode("content").ChildNodes;
            foreach(XmlNode node in list)
            {
                if (node.Attributes["name"].Value == txtSelect.Text)
                {
                    lblPerson.Text = node.Attributes["name"].Value;
                    lblQQ.Text = node.Attributes["qq"].Value;
                    lblEmail.Text = node.Attributes["email"].Value;
                    lblTel.Text = node.Attributes["tel"].Value;
                    lblContent.Text = node.InnerText;
                }
           
            }
        }
    }
}

原文地址:https://www.cnblogs.com/FeiyueHang/p/1880785.html