Professional Documents
Culture Documents
Create A Simple XML Document To Display The Address Book
Create A Simple XML Document To Display The Address Book
ASSIGNMENT II
Slot : III
Subject Code & Name : CSE2802 & XML and Web Services - WSDL
Mark
Marks
Content Details Split
Awarded
up
Aim (10) Suitable to title 10
Steps wise Description 10
Algorithm (20)
Logic Description 10
Version Declaration 5
Root Element 5
Sub Element 5
Number of child element 5
Program (40)
Number of attributes 5
Proper nesting 5
Element declaration 5
Attribute declaration 5
Opening element 5
Closing element 5
Concept (20) Application of xml 5
program
Attribute explanation 5
Output(10) Actual Output 10
Total = 100
Faculty HOD
191711190 || B.SATISH
A Simple XML Document To Display The Address Book
AIM:
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.
➢ 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.
➢ XML is a file extension for an Extensible Markup Language (XML) file
format used to create common information formats and share both the format
and the data on the World Wide Web, intranets, and elsewhere using standard
ASCII text.
➢ XML is similar to HTML. Both XML and HTML contain markup symbols
to describe the contents of a page or file. HTML, however, describes the
content of a Web page (mainly text and graphic images) only in terms of how
it is to be displayed and interacted with. For example, the letter "p" placed
within markup tags starts a new paragraph.
191711190 || B.SATISH
➢ XML describes the content in terms of what data is being described. For
example, the word "phonenum" placed within markup tags could indicate that
the data that followed was a phone number. An XML file can be processed
purely as data by a program or it can be stored with similar data on another
computer or it can be displayed, like an HTML file. For example, depending
on how the application in the receiving computer wanted to handle the phone
number, it could be stored, displayed, or dialed.
ALGORITHM :
➢ In the Navigator view, right-click the XML schema file that you want to work
with and select Generate > XML File.
➢ Select a project or folder to contain the XML file and type a name for the file
in the File Name field, then click Next. The name of the file must end in .xml.
➢ Select the Root element of the XML file. The root element of an XML file is
the element that contains all other elements in that file. All of the global
elements you have defined in the XML schema will be included in the Root
element list. If you do not have any global elements in your XML schema,
you cannot create an XML file from it.
191711190 || B.SATISH
Logic Description and its steps:
191711190 || B.SATISH
PROGRAM :
XML file:
Version
<guestbook>
</guestbook>---------Root Element
191711190 || B.SATISH
Version:
Root Element:
Sub Elements:
1.<entry name>……..</entry>
2.<contact Details>
i)name
ii)email
Number of Attributes:
1.Name
2.location
3.email
4.contact
5.comments
Nesting
Nesting is done in the entire program under the root element ,the sub elements are
declared under the sub elements many childs elements are declared this is known as
nesting.
191711190 || B.SATISH
Xml element declaration:
<Person xmlns='urn:w3.org:People'
xmlns:b='urn: w3.org:People:base'
xmlns:u='urn: w3.org:units' >
<name>satish</name>
<age b:base='10' u:units='years' >20</age>
</Person>
<html xmlns="http://www.w3.org/1999/xhtml">
191711190 || B.SATISH
<title>Guestbook</title>
<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>
<td colspan="2">
<font color="Silver">Guestbook</font>
</td>
</tr>
191711190 || B.SATISH
<tr>
border-top: red 1px solid; border-bottom: red 1px solid; border-left: red 1px solid;">
<tr>
<td colspan="2">
Visible="false"></asp:Label></td>
</tr>
<tr>
Name:</td>
</tr>
<tr>
191711190 || B.SATISH
Location:</td>
</tr>
<tr>
Email:</td>
</tr>
<tr>
ContactNo:</td>
<asp:TextBoxID="TextBoxContact"runat="server"
MaxLength="10"></asp:TextBox></td>
</tr>
<tr>
191711190 || B.SATISH
Comments:</td>
</tr>
<tr>
<td>
</td>
<td>
</td>
</tr>
</table>
</td>
<td width="70%">
191711190 || B.SATISH
<ItemTemplate>
<font color="#660066">Name:
</font>
<br />
<br />
<font color="#660066">Location:
</font>
<br />
<font color="#330000"">Date:
</font>
<br />
191711190 || B.SATISH
<font color="#660066">About <%# DataBinder.Eval(Container.DataItem,
"name") :</font><font color="black" face="Arial" size="2pt">
</font>
</ItemTemplate>
<SeparatorTemplate>
<td>
</td>
</tr>
</SeparatorTemplate>
</asp:DataList>
</td>
</tr>
</table>
</form>
</fieldset>
</body>
</html>
191711190 || B.SATISH
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;
BindData();
191711190 || B.SATISH
void BindData()
XmlTextReadermyXmlReader=new
XmlTextReader(Server.MapPath("guestbook.xml"));
myDataSet.ReadXml(myXmlReader);
myXmlReader.Close();
Guestbook.DataSource = myDataSet.Tables[0];
Guestbook.DataBind();
ErrorMessage.Visible = true;
else
191711190 || B.SATISH
// Open the XML doc
myXmlDocument.Load(Server.MapPath("guestbook.xml"));
System.Xml.XmlNodmyXmlNode=
myXmlDocument.DocumentElement.FirstChild;
System.Xml.XmlElementmyXmlElement=
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);
myXmlDocument.DocumentElement.InsertBefore(myXmlElement, myXmlNode);
myXmlDocument.Save(Server.MapPath("guestbook.xml"));
txtName.Text = "";
txtEmail.Text = "";
191711190 || B.SATISH
txtLocation.Text = "";
txtComments.Text = "";
TextBoxContact.Text = "";
BindData();}
txtName.Text = "";
txtEmail.Text = "";
txtLocation.Text = "";
txtComments.Text = "";
TextBoxContact.Text = "";
191711190 || B.SATISH
CONCEPT:
➢ The XML Recommendation states that an XML document has both logical
and physical structure. Physically, it is comprised of storage units called
entities, each of which may refer to other entities, similar to the way that
include works in the C language. Logically, an XML document consists of
declarations, elements, comments, character references, and processing
instructions, collectively known as the markup.
➢ An XML document consists of three parts, in the order given:
1.An XML declaration (which is technically optional, but recommended in
most normal cases)
2.A document type declaration that refers to a DTD (which is optional, but
required if you want validation)
3. A body or document instance (which is required)
Collectively, the XML declaration and the document type declaration are called the
XML prolog.
XML Declaration:
The XML declaration is a piece of markup (which may span multiple lines of a file)
that identifies this as an XML document. The declaration also indicates whether the
document can be validated by referring to an external Document Type Definition
(DTD). DTDs are the subject of chapter 4; for now, just think of a DTD as a set of
rules that describes the structure of an XML document.
191711190 || B.SATISH
The minimal XML declaration is:
XML is case-sensitive (more about this in the next subsection), so it's important that
you use lowercase for xml and version. The quotes around the value of the version
attribute are required, as are the ? characters. At the time of this writing, "1.0" is the
only acceptable value for the version attribute, but this is certain to change when a
subsequent version of the XML specification appears.
NOTE:
Do not include a space before the string xml or between the question mark and the
angle brackets. The strings <?xml and ?> must appear exactly as indicated. The
space before the ?> is optional. No blank lines or space may precede the XML
declaration; adding white space here can produce strange error messages.
➢ In most cases, this XML declaration is present. If so, it must be the very first
line of the document and must not have leading white space. This declaration
is technically optional; cases where it may be omitted include when
combining XML storage units to create a larger, composite document.
➢ This Extended Backus-Naur Form (EBNF) notation, characteristic of many
W3C specifications, means that an XML declaration consists of the literal
sequence '<?xml', followed by the required version information, followed by
optional encoding and standalone declarations, followed by an optional
amount of white space, and terminating with the literal sequence '?>'. In this
notation, a question mark not contained in quotes means that the term that
precedes it is optional.
191711190 || B.SATISH
➢ The optional encoding part of the declaration tells the XML processor (parser)
how to interpret the bytes based on a particular character set. The default
encoding is UTF-8, which is one of seven character-encoding schemes used
by the Unicode standard, also used as the default for Java. In UTF-8, one byte
is used to represent the most common characters and three bytes are used for
the less common special characters. UTF-8 is an efficient form of Unicode for
ASCII-based documents. In fact, UTF-8 is a superset of ASCII.3
The document type declaration follows the XML declaration. The purpose of this
declaration is to announce the root element (sometimes called the document
element) and to provide the location of the DTD.
191711190 || B.SATISH
➢ Let's start with a simple but common case. In this example, we are indicating
that the DTD and the XML document reside in the same directory (i.e., the
ExternalDeclarations are contained in the file employees.dtd) and that the root
element is Employees:
Document Body
➢ The document body, or instance, is the bulk of the information content of the
document. Whereas across multiple instances of a document of a given type
(as identified by the DOCTYPE) the XML prolog will remain constant, the
document body changes with each document instance (in general). This is
because the prolog defines (either directly or indirectly) the overall structure
while the body contains the real instance-specific data. Comparing this to data
structures in computer languages, the DTD referenced in the prolog is
analogous to a struct in the C language or a class definition in Java.
The DOCTYPE is "html" (not "xhtml"), so the document body begins with <html
....> and ends with </html>. Simple XHTML 1.0 Document with XML Prolog and
Document Body
<!DOCTYPE html
191711190 || B.SATISH
PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<head>
<title>XHTML 1.0</title>
</head>
<body>
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">DTD</a>.</p>
</body>
</html>
191711190 || B.SATISH
OUTPUT:
191711190 || B.SATISH