A Asp.net+xml message This example

Source: Internet
Author: User
Tags date comments date time stamp datetime goto header insert tostring
viewpost.aspx--View the message submitted
viewguestbook.aspx--, check all messages.
Header.inc
guestpost.aspx--message form and XML write operation
Footer.inc
Guest.xml--xml data

The source code is as follows:

Viewguestbook.aspx
<%@ Import namespace= "System"%>
<%@ Import namespace= "System.IO"%>
<%@ Import namespace= "System.Data"%>
<%@ Assembly name= "System.Xml"%>
<%@ Import namespace= "System.Xml"%>
<%@ Page language= "c<%--Needed assembiles--%>

<title>welcome to Saurabh ' s guestbook.</title>
<script language= "C//run" the script when the Page is Loaded
public void Page_Load (Object sender, EventArgs e)
{
A label, its use stated later
Tryagain:

The path to the Xml file which would contain all the data
Modify this if you are have any other file or directory mappings.
Modify this if your have been directed here's the ReadMe file.
string datafile = "Db/guest.xml";

Try-catch blocks to read in XML file
Try
{
Make a instance to the XmlDataDocument class
This class can read from the XML file in and ordered format
XmlDataDocument Datadoc = new XmlDataDocument ();

Infer the DataSet schema from the XML data and load the XML data
Datadoc. DataSet.ReadXML (New StreamReader (Server.MapPath (datafile)));

Databind the "the" in "the Dataset to the Repeter
Mydatalist.datasource = Datadoc. Dataset.tables[0]. DefaultView;
Mydatalist.databind ();

Free up the XML file to is used by other programs
Datadoc=null;


}
catch (IOException ed)
{
Here I am for now trying to overcome a bugs in my guestbook exapmle
The Bug is and only one class can either read or write to my XML
Data file at a time.
If the file is being used I some some other page (eg the guest Book viewing page)
Then an ioexception would be thrown
So to handle such situtations what we
If an IOException was thrown the page goes again to the Tryagain label
and tries to write again into the XML file
This goes on till finally the resource are freed and the XML file is written to.

Goto Tryagain;
}
catch (Exception edd)
{
Catch any other exceptions that occur
Errmess. Text= "cannot read from XML file because" +edd. ToString ();
}



}


</script>
<link href= "Mystyle.css" Type=text/css rel=stylesheet>

<body topmargin= "0" leftmargin= "0" marginwidth= "0" marginheight= "0" rightmargin= "0" >
<!--<asp:label id= "errmess" text= "style=" color:<br>

<asp:repeater id= "mydatalist" runat= "Server" >

<template name= "HeaderTemplate" >

<table class= "Mainheads" width= "100%" style= "font:8pt Verdana" >
<tr style= "Background-color: <th>
Name
</th>
<th>
Country
</th>
<th>
Email
</th>
<th>
Comments
</th>
<th>
Date/time
</th>
</tr>

</template>

<template name= "ItemTemplate" >

<tr style= "Background-color: <td>
<% </td>
<td>
<% </td>
<td>
<% </td>
<td>
<% </td>
<td>
<% </td>
</tr>

</template>

<template name= "FooterTemplate" >

</table>

</template>

</ASP:Repeater>

<!--</body>


Viewpost.aspx
<%@ Import namespace= "System"%>
<%@ Page language= "c<title>welcome to Saurabh ' s guestbook.</title>
<script language= "C//execute This script when the page loads
void Page_Load (Object Src, EventArgs E)
{
If the page is called from anothe page
if (! Page.IsPostBack) {
Get the diffrent Parameters from the Querry string and store it
to respective Labels
Namelabel.text = request.params["Name"];
countrylabel.text= request.params["Country"];
emaillabel.text=request.params["Email"];
commentslabel.text=request.params["Comments"];
}
if (Page.IsPostBack)
{
else display an error
Errmess. Text= "This Page cannot is called directly. It has to is called from the Guestbook posting page. ";
}
}
</script>
<link href= "Mystyle.css" Type=text/css rel=stylesheet>
<body topmargin= "0" leftmargin= "0" rightmargin= "0" marginwidth= "0" marginheight= "0" >
<!--<asp:label id= "errmess" text= "style=" COLOR: <center>
&LT;H2 class= "Newsbody" ><b>thank for your posting in my guestbook.</b><table align=center width= "60%" border= "0" cellspacing= "2" cellpadding= "1" >
<tr class= "titheading" ><td colspan= "2" >the information you posted!</td></tr>
<tr class= "Newsbody" >
<td>name:</td>
<td><asp:label id= "Namelabel" text= "" runat= "Server"/></td>
</tr>
<tr class= "Newsbody" >
<td>country:</td>
<td><asp:label id= "Countrylabel" text= "" runat= "Server"/></td>
</tr>
<tr class= "Newsbody" >
<td>e-mail:</td>
<td><asp:label id= "Emaillabel" text= "" runat= "Server"/></td>
</tr>
<tr class= "Newsbody" >
<td>comments:</td>
<td><asp:label id= "Commentslabel" text= "" runat= "Server"/></td>
</tr>
</table>
<br>

</center>
<!--
</body>


Guestpost.aspx

<%@ Import namespace= "System"%>
<%@ Page language= "c<%@ Import namespace= System.IO"%>
<%@ Assembly name= "System.Xml"%>
<%@ Import namespace= "System.Xml"%>
<%--These are the imported assembiles and namespaces need to run the guest book--%>


<title>welcome to Saurabh ' s guestbook.</title>
<script language= "C///<summary>
This methord was called the Submit button is clicked
</summary>
public void Submit_click (Object sender, EventArgs e)
{
A label need to the Goto statement explained below
Tryagain:

The path to the Xml file which would contain all the data
Modify this if you are have any other file or directory mappings.
Modify this if your have been directed here's the ReadMe file.
string datafile = "Db/guest.xml";

Put the posting code within a Try-catch block
try{

Proceed only if all the required feilds are filled-in
if (page.isvalid&&name.text!= "" &&country.text!= "" &&email.text!= "") {

Errmess. Text= "";

Make an instance of the class XmlDocument
XmlDocument xmldocument = new XmlDocument ();

Load the XML file you'll use as your database.
Since we are working on a server we have to use ' Server.MapPath () '
To mape the path to the database file

XmlDocument. Load (Server.MapPath (datafile));

Make an instance of Documentnavigator class which'll help us to
Navigate in the loaded XML data file.
Documentnavigator navigator = new Documentnavigator (XmlDocument);

Below code is very significant as it navigates through the XML document and
Stores the required values (Ps:read this code carefully)

The The XML documents elements (in I XML file this would come to the ' Guests ' Node
Navigator. Movetodocumentelement ();

Then insert a (firstchild) which'll contain all information
of a single guest posting
Navigator. Insert (Treeposition.firstchild, XmlNodeType.Element, "Guest", "", "");

Insert the Element of Name as the ' Guest '
Navigator. Insert (Treeposition.firstchild, XmlNodeType.Element, "Name", "", "");
This is important to specify what kind of Value'll the Name element contain
Navigator. Insert (Treeposition.firstchild, XmlNodeType.Text, "Name", "", "");
Assign the Name element the Value from the. Text property of the TextBox
Navigator. Value=name.text;

Go back to the Parent Node ie ' Guest '
Navigator. Movetoparent ();

Insert another Element ' Country ' after the firstchild ie. After the ' Name ' node.
Navigator. Insert (Treeposition.after, XmlNodeType.Element, "Country", "", "");
Navigator. Insert (Treeposition.firstchild, XmlNodeType.Text, "Country", "", "");
Navigator. Value=country.text;

Navigator. Movetoparent ();
Navigator. Insert (treeposition.after,xmlnodetype.element, "Email", "", "");
Navigator. Insert (Treeposition.firstchild, XmlNodeType.Text, "Email", "", "");
Navigator. Value=email.text;

Navigator. Movetoparent ();
Navigator. Insert (treeposition.after,xmlnodetype.element, "Comments", "", "");
Navigator. Insert (Treeposition.firstchild, XmlNodeType.Text, "comments", "", "");
Navigator. Value=comments.value;
Navigator. Movetoparent ();

Navigator. Insert (Treeposition.after, XmlNodeType.Element, "DateTime", "", "");
Navigator. Insert (Treeposition.firstchild, XmlNodeType.Text, "DateTime", "", "");
Set the Date time stamp of the entry
DateTime now = DateTime.Now;
Navigator. Value=now. ToShortDateString () + "" +now. Toshorttimestring ();

After making the necessary changes we Save the chenges to the Xml Document
XmlDocument. Save (Server.MapPath (datafile));
Free up of the XML file from the Document file, so, programs can use it
Xmldocument=null;

Build a custom Querry sending the data posted to another page for display
Since it is a querry we have to encode it in UTF8 format
String querystring = "Name=" + System.Web.HttpUtility.UrlEncodeToString (Name.text, System.Text.Encoding.UTF8);
QueryString + = "&&country=" + System.Web.HttpUtility.UrlEncodeToString (Country.text, SYSTEM.TEXT.ENCODING.UTF8);
QueryString + = "&&email=" + System.Web.HttpUtility.UrlEncodeToString (Email.text, System.Text.Encoding.UTF8) ;
QueryString + = "&&comments=" + System.Web.HttpUtility.UrlEncodeToString (Comments.value, SYSTEM.TEXT.ENCODING.UTF8);

The "Go" page viewpost.aspx and append the Querry string at its end.
Page.navigate ("Viewpost.aspx?" + querystring);

}
Else
{
If any of the feilds are kept empty show a error message
Errmess. Text= "Fill in the" required feilds of the guestbook. ";

}
}
catch (IOException ed)
{
Here I am for now trying to overcome a bugs in my guestbook exapmle
The Bug is and only one class can either read or write to my XML
Data file at a time.
If the file is being used I some some other page (eg the guest Book viewing page)
Then an ioexception would be thrown
So to handle such situtations what we
If an IOException was thrown the page goes again to the Tryagain label
and tries to write again into the XML file
This goes on till finally the resource are freed and the XML file is written to.
Goto Tryagain;
}
catch (Exception edd)
{
Catch any other exception that occur
Errmess. Text= "Cannot write to XML file because" +edd. ToString ();

}
}
</script>
<link href= "Mystyle.css" Type=text/css rel=stylesheet>
<body topmargin= "0" leftmargin= "0" rightmargin= "0" marginwidth= "0" marginheight= "0" >
<%--Include a header file ' Header.inc '--%>
<!--<br>

<br>
<asp:label id= "errmess" text= "style=" color: <form runat= "Server" >
<table border= "0" width= "80%" align= "Center" >
<tr >
&LT;TD class= "newsheading" ><b>sign-in my guestbook</b></td>
&LT;TD class= "newsheading" > </td>

</tr>
<tr class= "Newsbody" >
<td>name:</td>
&LT;TD ><asp:textbox text= "" id= "Name" runat= "Server"/> <font
</tr>
<tr class= "Newsbody" >
<td>country:</td>
<td><asp:textbox text= "" id= "Country" runat= "Server"/> <font
</tr>
<tr class= "Newsbody" >
<td>e-mail:</td>
<td><asp:textbox test= "" id= "Email" runat= "Server"/> <font
</tr>
<tr class= "Newsbody" >
<td>comments:</td>
<td><textarea id= "Comments" cols= "rows=" "4" runat= "Server"/></td>

</tr>
<tr class= "Newsbody" >
&LT;TD colspan= "2" >
<asp:button class= "newsheading" id= "write" text= "Submit" runat= "Server"/></td>

</tr>
</table>
</form>
<br>

<!--</body>


Related Article

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.