SIGN UP MEMBER LOGIN:    
ARTICLE

How to create an address book using XML

Posted by Purushottam Rathore Articles | ASP.NET Programming January 19, 2009
In this article I am going to describe how to crete an address book application using XML file.
Reader Level:
Download Files:
 

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="puru.rathore@gmail.com" 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="hrrr@yahoo.com" location="Hi" ContactNo="9958695874" date="1/16/2009 4:28:10 PM">Hello everybody...</entry>

<entry name="Dharmendra" email="dharmendra123@gmail.com" 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="smith.dk@hotmail.com" location="United State" ContactNo="9878458574" date="1/16/2009 3:25:28 PM">Hi this is Smith from US</entry>

<entry name="purushottam" email="puru_rock@gmail.com" 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.

Login to add your contents and source code to this article
share this article :
post comment
 
Nevron Gauge for SharePoint
Become a Sponsor
PREMIUM SPONSORS
  • Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites - Click Here!
    Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Nevron Gauge for SharePoint
Become a Sponsor