How to create an address book using XML


What is an Address Book?
 
An Address Book is the place where you store the necessary contact informations, email addresses of the contacts that you wish to be in regular touch with. It helps make sending emails more convenient. Addresses can be stored as Individuals and/or Groups. 
 
How can I use the Address Book?
 
To use your address book, you need to first add name, email,phone numbar, addresses etc of your contacts (family, friends, relatives etc.).
 
Here I am going to discuss how to create a address book in simple way by using XML file and how to create XML file at run time.

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_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 id="Head1" runat="server">
<title>Guestbook</title>
<%--JavaScript for display message.--%>
<script type="text/javascript">
function
disp_alert()
{
alert(
"Sorry! This is test application. You cann't send mail from here.");
}
</script>
</
head>
<
body>
<fieldset align="middle">
<legend><b>Guestbook</b></legend>
<form id="form1" runat="server">
<table width="100%" cellpadding="2" cellspacing="2">
<tr height="20" bgcolor="maroon">
<td colspan="2">
<font color="Silver">Guestbook</font>
</td>
</tr>
<tr>
<td width="30%" valign="top">
<table bgcolor="silver" width="100%" cellpadding="2" cellspacing="3" style="border-right:
red 1px solid;
border-top: red 1px solid; border-bottom: red 1px solid; border-left: red 1px solid;">
<tr>
<td colspan="2">
<asp:Label ID="ErrorMessage" runat="server" Font-Names="arial" Font-Size="14px" ForeColor="red"
Visible="false"></asp:Label></td>
</tr>
<tr>
<td style="width: 100px">
Name:</td>
<td style="width: 100px">
<asp:TextBox ID="txtName" runat="server"></asp:TextBox></td>
</tr>
<tr>
<td style="width: 100px">
Location:</td>
<td style="width: 100px">
<asp:TextBox ID="txtLocation" runat="server"></asp:TextBox></td>
</tr>
<tr>
<td style="width: 100px">
Email:</td>
<td style="width: 100px">
<asp:TextBox ID="txtEmail" runat="server"></asp:TextBox></td>
</tr>
<tr>
<td style="width: 100px">
ContactNo:</td>
<td style="width: 100px">
<asp:TextBox ID="TextBoxContact" runat="server" MaxLength="10"></asp:TextBox></td>
</tr>
<tr>
<td style="width: 100px">
Comments:</td>
<td style="width: 100px">
<asp:TextBox ID="txtComments" runat="server"></asp:TextBox></td>
</tr>
<tr>
<td>
</td>
<td>
<asp:Button ID="ButtonSubmite" runat="server" OnClick="Submit_Click" Text="Submit" />
<asp:Button ID="ButtonReset" runat="server" OnClick="Reset_Click" Text="Reset" />
</td>
</tr>
</table>
</td>
<td width="70%">
<asp:DataList ID="Guestbook" runat="server" Width="100%" AlternatingItemStyle-BackColor="lightgray"
BorderColor="red" BorderWidth="1pt" CellPadding="5" CellSpacing="5">
<ItemTemplate>
<font color="#660066">Name:
<%
# DataBinder.Eval(Container.DataItem, "name") %>
</font>
<br />
E-mail: <a href="#" onclick="disp_alert()">
<%# DataBinder.Eval(Container.DataItem, "email") %>
</a>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Contact No:
<%
# DataBinder.Eval(Container.DataItem, "ContactNo") %>
<br />
<font color="#660066">Location:
<%
# DataBinder.Eval(Container.DataItem, "location") %>
</font>
<br />
<font color="#330000"">Date:
<%
# DataBinder.Eval(Container.DataItem, "date") %>
</font>
<br />
<font color="#660066">About&nbsp;<%# DataBinder.Eval(Container.DataItem, "name")
>
:
</font><font color="black" face="Arial" size="2pt">
<%# DataBinder.Eval(Container.DataItem, "entry_Text") %>
</font>
</ItemTemplate>
<SeparatorTemplate>
<tr height="1px" bgcolor="red">
<td>
</td>
</tr>
</SeparatorTemplate>
</asp:DataList>
</td>
</tr>
</table>
</form>
</fieldset>
</
body>
</
html>

Default.aspx.cs:

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml;

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
BindData();
}
void BindData()
{
XmlTextReader myXmlReader = new XmlTextReader(Server.MapPath("guestbook.xml"));
DataSet myDataSet = new DataSet();
myDataSet.ReadXml(myXmlReader);
myXmlReader.Close();
Guestbook.DataSource = myDataSet.Tables[0];
Guestbook.DataBind();
}
protected void Submit_Click(object sender, EventArgs e)
{
if (txtName.Text == "" || txtEmail.Text == "" || txtLocation.Text == "" || txtComments.Text == "")
{
ErrorMessage.Text =
"Please fill all entries.";
ErrorMessage.Visible =
true;
}
else
{
// Open the XML doc
System.Xml.XmlDocument myXmlDocument = new System.Xml.XmlDocument();
myXmlDocument.Load(Server.MapPath(
"guestbook.xml"));
System.Xml.
XmlNode myXmlNode = myXmlDocument.DocumentElement.FirstChild;
// Create new XML element and populate its attributes
System.Xml.XmlElement myXmlElement = myXmlDocument.CreateElement("entry");
myXmlElement.SetAttribute(
"name", Server.HtmlEncode(txtName.Text));
myXmlElement.SetAttribute(
"email", Server.HtmlEncode(txtEmail.Text));
myXmlElement.SetAttribute(
"location", Server.HtmlEncode(txtLocation.Text));
myXmlElement.SetAttribute(
"ContactNo", Server.HtmlEncode(TextBoxContact.Text));
myXmlElement.SetAttribute(
"date", DateTime.Now.ToString());
myXmlElement.InnerText = Server.HtmlEncode(txtComments.Text);
// Insert data into the XML doc and save
myXmlDocument.DocumentElement.InsertBefore(myXmlElement, myXmlNode);
myXmlDocument.Save(Server.MapPath(
"guestbook.xml"));
txtName.Text =
"";
txtEmail.Text =
"";
txtLocation.Text =
"";
txtComments.Text =
"";
TextBoxContact.Text =
"";
BindData();
}
}
protected void Reset_Click(object sender, EventArgs e)
{
txtName.Text =
"";
txtEmail.Text =
"";
txtLocation.Text = "";
txtComments.Text =
"";
TextBoxContact.Text =
"";
}
}

Output:

Addressbook.JPG

XML file:

<?xml version="1.0" encoding="utf-8"?>

<guestbook>

<entry name="Purushottam rathore" email="[email protected]" location="Delhi" ContactNo="9858784585" date="1/19/2009 5:36:21 PM">Hi this is purushottam rathore from Delhi (India)</entry>

<entry name="Hello" email="[email protected]" location="Hi" ContactNo="9958695874" date="1/16/2009 4:28:10 PM">Hello everybody...</entry>

<entry name="Dharmendra" email="[email protected]" location="Noida" ContactNo="9685487584" date="1/16/2009 3:40:26 PM">Hi this is dharmendra.Hi this is dharmendra.Hi this is dharmendra.Hi this is dharmendra.Hi this is dharmendra.Hi this is dharmendra.</entry>

<entry name="smith" email="[email protected]" location="United State" ContactNo="9878458574" date="1/16/2009 3:25:28 PM">Hi this is Smith from US</entry>

<entry name="purushottam" email="[email protected]" location="Delhi" ContactNo="9586784521" date="1/16/2009 2:36:23 PM">Hi this is puru.</entry>

</guestbook>

Note: Download the attachment file to learn more about this article.

Up Next
    Ebook Download
    View all
    Learn
    View all