Signup · Login
Stardeveloper.com  
Home · Tutorials · Forums · ASP.NET Newsletter Application · Web Hosting Plans · Faisal Khan's Blog · Contact
Search Stardeveloper.com
Newsletter
Enter your email address to receive full length articles at Stardeveloper:


Article Categories
.NET  .NET
  ASP (16)
  ASP.NET (41)
  ADO (16)
  ADO.NET (11)
  COM (6)
  Web Services (4)
  C# (1)
  VB.NET (3)
  IIS (2)

J2EE  J2EE
  JSP (15)
  Servlets (9)
  Web Services (1)
  EJB (4)
  JDBC (4)
  E-Commerce (1)
  J2ME (1)
  Products (1)
  Applets (1)
  Patterns (1)

Main Category  Other
  Website Maintenance (3)
Log In
UserName Or Email:

Password:

Auto-Login:

Hosted by Securewebs.com
 
Home : .NET : ASP.NET : Introduction to XML
 
Read full length articles at Stardeveloper using Twitter Follow on Twitter Facebook Facebook fan page Email Get Articles via Email RSS Get Articles via RSS Feed

Introduction to XML

by Faisal Khan.

Introduction
XML stands for Extensible Markup Language. An XML document stores data in the form of text. The data itself can be textual or binary. The binary data is not stored as binary data but is first converted to and is stored as text data. Elements and attributes are used in XML document to encapsulate data in a more logical hierarchical fashion.

This tutorial is 1st one in the series of tutorials about XML and ASP.NET:

Sample XML Document
Following is a simple XML document:

<?xml version="1.0" encoding="utf-8"?>
<article>
	<author isadmin="true">Faisal Khan</author>
	<title>Sample XML Document</title>
	<body>The body of the article goes here.</body>
</article>

XML Declaration
All XML documents start with <?xml version="1.0" ?>. This tells the XML parser that what is to follow is an XML document. An optional encoding attribute is often added as well.

<?xml version="1.0" encoding="utf-8"?>
<article>
	<author isadmin="true">Faisal Khan</author>
	<title>Sample XML Document</title>
	<body>The body of the article goes here.</body>
</article>

Storing Data in an XML Document
The XML document contains text data that is held in its place by a logical hierarchy of elements. The data in the sample XML document, above, is highlighted below:

<?xml version="1.0" encoding="utf-8"?>
<article>
	<author isadmin="true">Faisal Khan</author>
	<title>Sample XML Document</title>
	<body>The body of the article goes here.</body>
</article>

Elements
All XML documents must have a root element. In our sample XML document, the root element is article. All elements must have a starting tag and an ending tag. The name of the element can be anything you want. But it is recommended to keep the element names short and simple to understand e.g., article, full_name, first_name, etc. If an element name has to consist of two words, it is recommended to insert an '_' (underscore) character in the place of space e.g., first_name for "first name". An element is contained between < and > characters. The ending tag has an additional slash '/' just before the name of the element.

The elements in the sample XML document are highlighted below:

<?xml version="1.0" encoding="utf-8"?>
<article>
	<author isadmin="true">Faisal Khan</author>
	<title>Sample XML Document</title>
	<body>The body of the article goes here.</body>
</article>

Attributes
An attribute is name/value pair within an element that can store further information for an element. It has to be in the format of name="value". In our XML document, isadmin is the name of the attribute within author element, whose value is "true". The attribute in our XML document is highlighted below:

<?xml version="1.0" encoding="utf-8"?>
<article>
	<author isadmin="true">Faisal Khan</author>
	<title>Sample XML Document</title>
	<body>The body of the article goes here.</body>
</article>

Entity References
Certain characters are illegal to be present in data segments of the elements i.e., between element start and end tags. These characters are &, <, >, ', and ". The reason is that they have special meaning in an XML document. As you read above, XML elements and attributes use these characters to encapsulate data. Now what should you do if you want to use these characters as data in an XML document? Well, make use of entity references.

An "entity" is a name/value pair defined in a DTD (Document Type Definition) file. We will learn more about DTD files later. For now, you should know that an entity can be a name/value pair, just like an attribute, which you can define yourself. While attributes are defined in XML documents, entities (1 ore more) are defined inside DTD files. You can define an entity in an XML document using DTD syntax and make use of it later in the data in your XML document using an entity reference who syntax is: &entityName;. This is dynamic substitution. The XML parser will replace the entity reference at runtime with the value of that entity.

<!ENTITY websiteName "Stardeveloper.com">

Above code defines an entity "websiteName" with the value, "Stardeveloper.com". You can access the value of this entity using an entity reference within your XML document using the syntax &websiteName; as shown below:

<?xml version="1.0" encoding="utf-8"?>
<article>
	<author isadmin="true">Faisal Khan</author>
	<title>Sample XML Document</title>
	<body>This article will be posted at &websiteName;.</body>
</article>

 ( 1 Remaining ) Next

Comments/Questions

No Comments Found.


Post Comments/Questions

In order to post questions/comments, you must be logged-in. If you are not a member yet, then signup, otherwise login. Once you login then come back to this page and you'll see a form right here which will allow you to post comments/questions.

Please note, one of the benefits of signing up is to be notified immediately by email everytime you receive a reply to the thread you have subscribed.

 
© 1999 - 2010 Stardeveloper.com, All Rights Reserved.