Use a guest book to illustrate the complete process of C # manipulating XML

Source: Internet
Author: User
Tags count eval generator mail tostring visual studio
xml| process
First step: Create the DB folder in the project file and create the Db.xml document in the DB folder. Db.xml structure contains only:<group></group>
Step Two: Create a new message and display two images: Messageadd.aspx and messagelist.aspx. Below are two pages of foreground and background source.
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= "Song Body" >
<P> </P>
<p align= "center" >
<table class= "Txt1" id= "Table1" cellspacing= "1" cellpadding= "4" width= "749" bgcolor= "#efefef"
border= "0" >
<TR>
&LT;TD bgcolor= "#e8dabd" > Your name </TD>
&LT;TD style= "width:508px" bgcolor= "#ffffff" ><asp:textbox id= "Textboxuname" runat= "Server" leavesql= "False" Leaveformat= "false" Htmlencode= "false"
Width= "519px" maxlength= "ten" cssclass= "Textareaclass" ></asp:textbox></TD>
&LT;TD bgcolor= "#ffffff" colspan= "1" rowspan= "1" ></TD>
</TR>
<TR>
&LT;TD bgcolor= "#e8dabd" colspan= "1" rowspan= "1" >E-Mail</TD>
&LT;TD style= "width:505px" bgcolor= "#ffffff" >
<p><asp:textbox id= "Textboxmail" runat= "Server" leavesql= "false" leaveformat= "false" Htmlencode= "false"
Width= "519px" maxlength= "cssclass=" Textareaclass "></asp:textbox></P>
</TD>
&LT;TD bgcolor= "#ffffff" colspan= "1" rowspan= "1" ><BR>
</TD>
</TR>
<TR>
&LT;TD bgcolor= "#e8dabd" colspan= "1" rowspan= "1" > Content </TD>
&LT;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>
&LT;TD bgcolor= "#ffffff" colspan= "1" rowspan= "1" ></TD>
</TR>
</TABLE>
</P>
<p align= "center" ><asp:button id= "Button1" runat= "Server" cssclass= "Textareaclass" text= "Submit" ></asp: Button>
<input class= "Textareaclass" onclick= "Window.location.replace (messagelist.aspx)" type= "button"
Value= "Back" ></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>
Summary description of the 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: This call is required for the ASP.net Web forms Designer.
//
InitializeComponent ();
Base. OnInit (e);
}

<summary>
Designer supports required methods-do not use the Code editor to modify
The contents of this method.
</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= "[message]" navigateurl= "messageadd.aspx" ></asp: Hyperlink>
</P>
<p align= "center" ><font face= "song Body" ></font><font face= "song Body" ></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" >
&LT;TD style= "width:200px" bgcolor= "#ffffff" ><STRONG> name:</strong>
<asp:label id=label5 runat= "Server" text=<%# DataBinder.Eval (Container, "Dataitem.username"). ToString ()%>>
</asp:Label>
</TD>
&LT;TD style= "width:150px" bgcolor= "#ffffff" ><strong><font face= "song body" > Time </font></strong ><strong><font face= "Song Body" >:</FONT></STRONG>
<asp:label id=label4 runat= "Server" text=<%# DataBinder.Eval (Container, "Dataitem.sendtime"). ToString ()%>>
</asp:Label></TD>
&LT;TD bgcolor= "#ffffff" ><strong>e-mail</strong><strong&gt:
<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" >
&LT;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>
Summary description of the 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: This call is required for the ASP.net Web forms Designer.
//
InitializeComponent ();
Base. OnInit (e);
}

<summary>
Designer supports required methods-do not use the Code editor to modify
The contents of this method.
</summary>
private void InitializeComponent ()
{
This. Load + = new System.EventHandler (this. Page_Load);
}
#endregion
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.