Saturday, June 23, 2012

Introduction to XML :xml in asp.net with exampale




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 . 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>



Now that you know what entities are and how entity references can be used to access their values inside the data portions of an XML document, dynamically; you should know that all XML documents can use 5 pre-loaded entity references as given below:
Character Entity Reference
& &amp;
< &lt;
> &gt;
' &apos;
" &quot;
Now, if you want to use any of the 5 special characters as data in your XML document, you can use them using entity references like this:

<?xml version="1.0" encoding="utf-8"?>
<article>
	<author isadmin="true">Faisal Khan</author>
	<title>Sample XML Document</title>
	<body>Now you can use &amp;, &lt;, &gt;, &apos;, and &quot;
 tags as often as you want.</body>
</article>


No comments:

Post a Comment