DIV CSS 佈局教程網

 DIV+CSS佈局教程網 >> 網頁腳本 >> XML學習教程 >> XML詳解 >> 用一個留言簿說明C#操作XML的完全過程
用一個留言簿說明C#操作XML的完全過程
編輯:XML詳解     
NT color=#800000>  第一步:在項目文件裡創建db文件夾,在db文件夾裡創建db.xml文檔。db.XML結構只包含:<Group></Group>

  第二步:新建添加留言和顯示留言的兩個畫面:MessageAdd.aspx和MessageList.ASPx。下面是兩個頁面的  前台和後台源碼。

  1.MessageAdd.ASPx

<%@ Page language="c#" Codebehind="MessageAdd.ASPx.cs" AutoEventWireup="false" Inherits="XMLGuest.MessageAdd" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD Html 4.0 Transitional//EN" >
<Html>
 <HEAD>
  <title>MessageAdd</title>
  <meta content="Microsoft Visual Studio 7.0" name="GENERATOR">
  <meta content="C#" name="CODE_LANGUAGE">
  <meta content="JavaScript" name="vs_defaultClIEntScript">
  <meta content="http://schemas.microsoft.com/intellisense/IE5" name="vs_targetSchema">
  <style>.Txt1 {
 FONT-SIZE: 12px
}
  </style>
 </HEAD>
 <body>
  <form id="MessageAdd" method="post" runat="server">
   <FONT face="宋體">
    <P>&nbsp;</P>
    <P align="center">
     <TABLE class="Txt1" id="Table1" cellSpacing="1" cellPadding="4" width="749" bgColor="#efefef"
      border="0">
      <TR>
       <TD bgColor="#e8dabd">您的大名</TD>
       <TD style="WIDTH: 508px" bgColor="#ffffff"><ASP:textbox id="TextBoxUName" runat="server" LeaveSQL="False" LeaveFormat="False" HtmlEncode="False"
         Width="519px" MaxLength="10" CSSClass="TextAreaClass"></ASP:textbox></TD>
       <TD bgColor="#ffffff" colSpan="1" rowSpan="1"></TD>
      </TR>
      <TR>
       <TD bgColor="#e8dabd" colSpan="1" rowSpan="1">E-Mail</TD>
       <TD style="WIDTH: 505px" bgColor="#ffffff">
        <P><ASP:textbox id="TextBoxMail" runat="server" LeaveSQL="False" LeaveFormat="False" HtmlEncode="False"
          Width="519px" MaxLength="30" CSSClass="TextAreaClass"></ASP:textbox></P>
       </TD>
       <TD bgColor="#ffffff" colSpan="1" rowSpan="1"><BR>
       </TD>
      </TR>
      <TR>
       <TD bgColor="#e8dabd" colSpan="1" rowSpan="1">內容</TD>
       <TD style="WIDTH: 505px" bgColor="#ffffff">
        <P><ASP:textbox id="TextBoxTxt" runat="server" LeaveSQL="False" LeaveFormat="True" HtmlEncode="False"
          Width="519px" TextMode="MultiLine" Height="160px"></ASP:textbox></P>
       </TD>
       <TD bgColor="#ffffff" colSpan="1" rowSpan="1"></TD>
      </TR>
     </TABLE>
    </P>
    <P align="center"><asp:button id="Button1" runat="server" CSSClass="TextAreaClass" Text=" 提交 "></ASP:button>&nbsp;&nbsp;&nbsp;&nbsp;
     &nbsp; <INPUT class="TextAreaClass" onclick="window.location.replace('MessageList.ASPx')" type="button"
      value=" 返回 "></P>
   </FONT>
  </form>
 </body>
</Html>

MessageAdd.cs

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.XML;

namespace XMLGuest
{
 /// <summary>
 /// MessageAdd 的摘要說明。
 /// </summary>
 public class MessageAdd : System.Web.UI.Page
 {
  protected System.Web.UI.WebControls.TextBox TextBoxUName;
  protected System.Web.UI.WebControls.TextBox TextBoxMail;
  protected System.Web.UI.WebControls.TextBox TextBoxTxt;
  protected System.Web.UI.WebControls.Button Button1;
 
  private void Page_Load(object sender, System.EventArgs e)
  {
   
  }

  #region Web Form Designer generated code
  override protected void OnInit(EventArgs e)
  {
   //
   // CODEGEN:該調用是 ASP.Net Web 窗體設計器所必需的。
   //
   InitializeComponent();
   base.OnInit(e);
  }
  
  /// <summary>
  /// 設計器支持所需的方法 - 不要使用代碼編輯器修改
  /// 此方法的內容。
  /// </summary>
  private void InitializeComponent()
  {   
   this.Button1.Click += new System.EventHandler(this.Button1_Click);
   this.Load += new System.EventHandler(this.Page_Load);

  }
  #endregion

  private void Button1_Click(object sender, System.EventArgs e)
  {
   
    XmlDocument doc = new XMLDocument();
    doc.Load(MapPath("db\\db.XML"));
    XMLNode node = doc.CreateElement("Item");
     
    XmlAttribute XMLattr = doc.CreateAttribute("UserName");
    xmlattr.Value = XMLConvert.DecodeName(TextBoxUName.Text);
    node.Attributes.Append(XMLattr);

    XMLattr = doc.CreateAttribute("UserEmail");
    xmlattr.Value = XMLConvert.DecodeName(TextBoxMail.Text);
    node.Attributes.Append(XMLattr);

    XMLattr = doc.CreateAttribute("Content");
    xmlattr.Value = XMLConvert.DecodeName(TextBoxTxt.Text);
    node.Attributes.Append(XMLattr);

    XMLattr = doc.CreateAttribute("SendTime");
    xmlattr.Value = XMLConvert.DecodeName(DateTime.Now.ToString());
    node.Attributes.Append(XMLattr);

    doc.ChildNodes[0].AppendChild(node);
    doc.Save(MapPath("db\\db.XML"));
    Response.Redirect("MessageList.ASPx");
    
  }
 }
}

2.MessageList.ASPx

<%@ Page language="c#" Codebehind="MessageList.ASPx.cs" AutoEventWireup="false" Inherits="XMLGuest.MessageList" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD Html 4.0 Transitional//EN" >
<Html>
 <HEAD>
  <title>MessageList</title>
  <meta name="GENERATOR" Content="Microsoft Visual Studio 7.0">
  <meta name="CODE_LANGUAGE" Content="C#">
  <meta name="vs_defaultClIEntScript" content="JavaScript">
  <meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/IE5">
  <style>
  .body { FONT-SIZE: 12px }
  .td1 { FONT-SIZE: 12px }
  </style>
 </HEAD>
 <body class="body">
  <form id="MessageList" method="post" runat="server">
   <P align="right">
    <asp:HyperLink id="GrowHyperLink1" runat="server" Text="[留言]" NavigateUrl="MessageAdd.aspx"></ASP:HyperLink>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
   </P>
   <P align="center"><FONT face="宋體"></FONT><FONT face="宋體"></FONT><BR>
    <ASP:DataGrid id="DataList1" runat="server" AutoGenerateColumns="False" ShowHeader="False" PageSize="15"
     BorderStyle="None" BorderWidth="0px" CellPadding="4" Font-Size="12px">
     <Columns>
      <ASP:TemplateColumn>
       <ItemTemplate>
        <TABLE id="Table7" borderColor="#efefef" cellSpacing="0" cellPadding="4" width="749" bgColor="#efefef"
         border="1">
         <TR class="td1" bgColor="#fafafa">
          <TD style="WIDTH: 200px" bgColor="#ffffff"><STRONG>姓名:</STRONG>
           <ASP:Label id=Label5 runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.UserName").ToString() %>'>
          </ASP:Label>&nbsp;&nbsp;
          </TD>
          <TD style="WIDTH: 150px" bgColor="#ffffff"><STRONG><FONT face="宋體">時間</FONT></STRONG><STRONG><FONT face="宋體">:</FONT></STRONG>
           <ASP:Label id=Label4 runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.SendTime").ToString() %>'>
           </ASP:Label></TD>
          <TD bgColor="#ffffff"><STRONG>E-Mail</STRONG><STRONG>:
            <ASP:HyperLink id=HyperLink1 runat="server" NavigateUrl='<%# "mailto:"+DataBinder.Eval(Container, "DataItem.UserEmail").ToString() %>' Text='<%# DataBinder.Eval(Container, "DataItem.UserEmail").ToString() %>'>
            </ASP:HyperLink></STRONG></TD>
         </TR>
         <TR class="td1">
          <TD bgColor="#ffffff" colSpan="3">
           <ASP:Label id=Label1 runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.Content").ToString() %>'>
           </ASP:Label></TD>
         </TR>
        </TABLE>
       </ItemTemplate>
      </ASP:TemplateColumn>
     </Columns>
     <PagerStyle Visible="False"></PagerStyle>
    </ASP:DataGrid>
   </P>
  </form>
 </body>
</Html>

MessageList.cs

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.IO;
using System.XML;

namespace XMLGuest
{
 /// <summary>
 /// MessageList 的摘要說明。
 /// </summary>
 public class MessageList : System.Web.UI.Page
 {
  protected HyperLink GrowHyperLink1;
  protected System.Web.UI.WebControls.DataGrid DataList1;
 
  private void Page_Load(object sender, System.EventArgs e)
  {
   FileStream stream = new FileStream(Server.MapPath("db\\db.XML"),FileMode.Open);
   try
   {
    XmlDataDocument doc = new XMLDataDocument();
    doc.DataSet.ReadXML(new StreamReader(stream));
    if(doc.DataSet.Tables.Count != 0 && doc.DataSet.Tables[0].Rows.Count!=0)
    {
     DataList1.DataSource = doc.DataSet.Tables[0].DefaultVIEw ;
     DataList1.DataBind();
    }
    stream.Close();
   }
   catch(Exception ding)
   {
    Response.Write(ding.Message);
   }
   finally
   {
    stream.Close();
   }
  }

  #region Web Form Designer generated code
  override protected void OnInit(EventArgs e)
  {
   //
   // CODEGEN:該調用是 ASP.Net Web 窗體設計器所必需的。
   //
   InitializeComponent();
   base.OnInit(e);
  }
  
  /// <summary>
  /// 設計器支持所需的方法 - 不要使用代碼編輯器修改
  /// 此方法的內容。
  /// </summary>
  private void InitializeComponent()
  {   
   this.Load += new System.EventHandler(this.Page_Load);

  }
  #endregion
 }
}

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved