Blue Theme Orange Theme Green Theme Red Theme
 
Home | Forums | ASP.NET 2.0 Tutorials | Web Services | How Do I...? | Class Browser | WPF Quick Starts
 | Consulting  
Submit an Article Submit a Blog 
 Jump to
Skip Navigation Links
TechnologyExpand Technology
WebsiteExpand Website
 Resources  
Close
 Our Network  
Close
Search :       Advanced Search »
Home » XML » A guided tour to XML

A guided tour to XML


XML (Extensible Markup Language) is a flexible way to create common information formats and share both the format and the data on the World Wide Web, intranets, and elsewhere.

Author Rank:
Total page views :  2480
Total downloads : 
   Print Read/Post comments Post a comment  Similar Articles  
   Email to a friend  Bookmark  Author's other articles  
 
Become a Sponsor


In this article I m going to show some information about XML. Like as waht is XML, how we use it etc.

XML

XML stands for Extensive Markup Langauge.
The ADO.NET and XML.NET  Framework Application Programming Interface (API) combination provides a unified way to work with XML in the Microsoft .NET Framework.  XML is a member of SGML and an extended version of HTML. Unlike HTML, XML stores and exchange the data. In HTML we can work with limited number of tags while in XML we can define our own tags.

 

The following example shows that how we can write our XML program.

The first line of an XML file is: <? Version ="1.0" ? > This line defines the XML version of the document. This tag tells the browser to start executing the file.

<?xml version="1.0" ? >
<bookstore>

    <book>

        <title> The Autobiography of Benjamin Franklin</title>

         <author>

            <first-name>Benjamin</ first-name >

            <last-name>Franklin</last-name>

         </author>

        <price> 8.99</price>

    </book>

    <book>

        <title> The Confidence Man</title>

         <author>

            <first-name>Herman</ first-name >

            <last-name>Melville</last-name>

         </author>

        <price> 11.99</price>

    </book>

</bookstore>


When view this document in browser, the output looks like this:

<?xml version="1.0" ? >
<
bookstore>

    <book>

        <title> The Autobiography of Benjamin Franklin</title>

         <author>

            <first-name>Benjamin</ first-name >

            <last-name>Franklin</last-name>

         </author>

        <price> 8.99</price>

    </book>

    <book>

        <title> The Confidence Man</title>

         <author>

            <first-name>Herman</ first-name >

            <last-name>Melville</last-name>

         </author>

        <price> 11.99</price>

    </book>

</bookstore>

 

Browser recognizes the XML and colors it appropriately. In this above example <bookstore> is the root node. Every XML document must start with a root node with the starting tag and end with root node ending tag; otherwise XML parser gives an error.

Important Characteristics of XML

XML is a case sensitive. In XML <BOOKS> and <books> are two different tags. All tags in XML must be well formed and must have a closing tag. Improper nesting of tags in XML won't parse the document properly.

For Example:   
<b> <i>Bold and Italic </b></i>

This is not well form. The well formed nesting tags will be:

<b>

  <i>Bold and Italic</i>

</b>

 

XML Parser

 

An XML parser is a program that sits between XML document and the application using the document. The job of a parser is to make sure the documents meet the defined structures, validation and constraints. We can define validation rule and constraints in a document type definition (DTD).

 

XML Namespaces

User can define an XML document's element names; it's possible that many developers will use the same name. XML namespaces allow developers to write a unique name and avoid conflicts between element names with other developers. With the help of URI, a namespace ensure the uniqueness of XML elements, tags and attribute.

The scope of a document's element depends on the URI. The following example shows a XML document with namespace.

 

<?xml version="1.0" ? >
<
book xmlns="http://wwww.c-sharpcorner.com/Images">

   <title> The Autobiography of Benjim Franklin</title>

   <author>

       <first-name>Benjamin</ first-name >

        <last-name>Franklin</last-name>

   </author>

   <price> 8.99</price>

</book> 

 

DTD(Document Type Definition)

 

A Document Type Definition (DTD).defines a document structure with a list of legal elements.  We can declare DTDs inline or as a link to an external file. We can also use DTD s to validate XML documents. This example shows the DTDs:

<! ELEMENT Two (#PCDATA)>

<! ELEMENT One (B)>

<! ATTLIST One c CDATA # REQUIRED>

This DTD define a format of data. The following XML is valid because the tag<two> is inside the tag<One>:

<One c="Attrib">

  <Two> Text</Two>

</One>

An XML schema describes the relationship between a document's elements and attributes. XML schemas describe the rules, which can be applied to any XML document, for elements and attributes.  If an XML documents references a schema and it does not meet the criteria, XML parser will give an error during parsing.

A schema starts with a<xsd:schema> tag and ends with </xsd:schema> tag. All schema items have the prefix xsd. The xmlns=http://www.w3.org/2001/XMLSchema is a http://www.w3.org URI, which indicates the schema should be interpreted according to the default namespace of the w3c.

The next piece of this line is the  target namespace, which indicates the location of a machine (a URI). Following example shows a schema:

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">

 

  <xsd:element name="bookstore" type="bookstoretype"/>

 

  <xsd:complexType name="book StoreType">

    <xsd:sequence maxoccurs="unbounded">

      <xsd:element name="book" type="boomtype"/>

    </xsd:sequence>

  </xsd:complexType>

 

  <xsd:complexType name="book Type">

    <xsd:sequence maxoccurs="unbounded">

      <xsd:element name="title"    type="xsd:string"/>

      <xsd:element name="author" type="xsd:authorname"/>

      <xsd:element name="price" type="xsd:decimal"/>

    </xsd:sequence>

    <xsd:attribute name="genre" type="xsd:string">

    </xsd:complexType>

 

  <xsd:complexType name="authorname">

    <xsd:sequence >

      <xsd:element name="first-name" type="xsd:string"/>

      <xsd:element name="last-name" type="xsd:string"/>

    </xsd:sequence>

  </xsd:complexType>

 

</xsd:schema> 

 

Extensible Hypertext Markup Language (XHTML)

 

Extensible Hypertext Markup Language (XHTML) is a next-generation language of HTML. XHTML is a better and improved version of HTML. XHTML is a combination of XML and HTML. XHTML uses elements of HTML 4.01 and rules of XML to provide a more consistent, well formed and organized language.

 

An XML Documents and Its Items

 

An XML document is a set of elements in a well formed and valid standard format. A document is valid if it has a DTD associated with it and if it compiles with the DTD.

An XML document has the following parts:

 

  1. Prolog
  2. DOCTYPE declaration
  3. Start and end tags
  4. Comments
  5. Character and entity references
  6. Empty Elements
  7. Processing Instructions
  8. CDATA section
  9. Attributes
  10. White Spaces  

Prolog

 

The prolog part of a document appears before the root tag. The prolog information applies to the entire document. It can have character encoding, stylesheets, comments and processing instructions. Let see the prolog with the following example.

 

<? Version ="1.0" ? >

<? Xml-stylesheet type ="text/xsl" href="books.xsl ?>
   <!
DOCTYPE StudentRecord  SYSTEM "mydtd.dtd">

<!=my comments-->

 

DOCTYPE Declaration

 

With the help of a DOCTYPE declaration, we can read the structure of our root element and DTD from external files. A DOCTYPE declaration can contain a root element or a DTD. In a validating environment, a DOCTYPE declaration is must.  In a DOCTYPE reference, we can even use a URI reference. See the following example:

<! DOCTYPE rootElement>

 

  or

 

  <! DOCTYPE rootElement SYSTEM ='URIreference'>

 

  Or

 

  <!DOCTYPE StudentRecord SYSTEM ="Å“mydtd.dtd'> 

 

Start and End Tags

Start and End tag are the heart of XML language. If we want to start a tag like as <book> to our XML file them it will end with </book>

 

Comments

Using comments in code is a good programming practice. Comments help us to understand with our coding. We can use comment like as:

<!--     My Comment Here -->

XML parsers ignore the comments.

 

CDATA Section

 

What if we want to use <and> characters in our XML file but not as a part of a tag?Well, we can not use them because XML parser will interpret them as start and end tags. CDATA provides the following solution, so we can use XML markup characters in your documents and have the XML parser ignore them. See the following example how we will use CDATA.

<! [CDATA [I want to use < and > , characters]]>

The parser will treat those characters as data. Another good example of CDATA is:

<![CDATA [<Title> This is the title of a page</Title>

 

In this case, the parser will treat the second Title as data, not as a markup tag.

 

Empty Elements

 

Empty elements start and end with same tag. They start with <and end with>. The text between these two symbols is the text data. For example:

<Name> </Name>

<IMG  SRC="img.jpg">

  <tagname/>

 

All these are empty element example. The <IMG> specifies an inline image, and the SRC attribute specifies the image’s location. The image can be any format, though browsers generally support only GIF, JPEG and PNG images.

 

Processing Instructions

 

Processing Instructions (PIs) play a vital role in XML parsing. A PI holds the parsing instructions, which are read by the parser and other programs.

 

<?xml version="1.0" ? >

All PIs start with <? And end with ?> 

 

Attributes

 

Attribute you add extra information to an element without creating another element. An attribute is a name and value pair. Both the name and value must be present in an attribute. The attribute value must be in double quotes; otherwise the parser will give the error. Let see the attribute in table tag. In this following example:

 <table border ="1" width="43%">

  <tr>

     <td width ="50%"> Row1, Column1</td>

     <td width ="50%"> Row1, Column2</td>

   </tr>

   <tr>

     <td width ="50%"> Row2, Column1</td>

     <td width ="50%"> Row2, Column2</td>

   </tr>

</table>

 

White Spaces

XML preserves white spaces except in attribute values. That means white space in our document will be displayed in browser. However white space are not allowed before the XML declaration. The XML parser reports all white spaces available in the document. If white space appear before declaration, the parser treats them as a PI. 


Login to add your contents and source code to this article
 About the author
 
Rahul Kumar Saxena
Rahul shows great interests in working with Microsoft technologies. He specializes in the implementation of DataBase & Graphics. His area of expertise includes: C#, ASP.NET,ADO.NET,Windows Forms & Web Services. He hails from background , Master's in Computer Application. With programming he loves photography, traveling and reading books.
(Talabpur*)
Looking for C# Consulting?
C# Consulting is founded in 2002 by the founders of C# Corner. Unlike a traditional consulting company, our consultants are well-known experts in .NET and many of them are MVPs, authors, and trainers. We specialize in Microsoft .NET development and utilize Agile Development and Extreme Programming practices to provide fast pace quick turnaround results. Our software development model is a mix of Agile Development, traditional SDLC, and Waterfall models.
Click here to learn more about C# Consulting.
 
Introducing MaxV - one click. infinite control. Hyper-V Hosting from MaximumASP.
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.
Dynamic PDF
ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
SQL and .NET performance profiling in one place
Investigate SQL and .NET code side-by-side with ANTS Performance Profiler 6, so you can see which is causing the problem without switching tools.
Go.NET
Build custom interactive diagrams, network, workflow editors, flowcharts, or software design tools. Includes many predefined kinds of nodes, links, and basic shapes. Supports layers, scrolling, zooming, selection, drag-and-drop, clipboard, in-place editing, tooltips, grids, printing, overview window, palette. 100% implemented in C# as a managed .NET Control. Document/View/Tool architecture with many properties&events. Optional automatic layout.
Dundas Software
Dundas Chart for .NET is the most advanced .NET charting package available today.  With an extremely complete feature set, elegant architecture and easy implementation, Dundas Chart can quickly add advanced Charting functionality to enhance and transform ASP.NET and Windows Forms applications.  Whether you are implementing charting into internal projects, or building applications for clients, Dundas Chart offers advanced technology and advanced results to get the most out of data.
60 FREE UI Controls from DevExpress
Register for your FREE copy on over 60 free presentation controls from DevExpress - Absolutely Free-of-Charge without any royalties or distribution costs. Visit Devexpress.com/60 today. Free controls include advanced lists box, dropdown calendar, rich text edit, spin edit, tab control and so much more!

DevExpress engineers feature rich presentation controls and reporting tools for WinForms, ASP.NET, WPF, and Silverlight. Our technologies help you build your best, see complex software with greater clarity and deliver compelling business solutions for Windows and the web in the shortest possible time.
Clickatell's SMS Gateway
Clickatell's Developer Solutions allow you to SMS enable any website or application via a range of API's. Learn More about our API connections.
Free access to .NET Memory Management video
Everything you need to know about Garbage Collection, Temporary Objects, Fragmentation, Finalization and common causes of memory leaks in .NET. Watch the video here.
Microsoft Visual Studio 2010
Visualize your workspace with new multiple monitor support, powerful Web development, new SharePoint support with tons of templates and Web parts, and more accurate targeting of any version of the .NET Framework. Get set to unleash your creativity.
Nevron Chart for .NET 2010.1 Now Available
The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
Developer-Ready ASP.NET 2.0 Web Hosting with 3 MONTHS FREE
Now supporting .NET 3.0 Framework with Windows Workflow Foundation, Windows Communication Foundation (WCF), Windows Presentation Foundation (WPF), windows CardSpace (WCS)! Providing more flexibility for Developers with Web Services Support and a User/Permission Manger. Also supporting MS SQL 2005/2000 with Real-Time Backups, FREE Automated Attach .MDF Tool, FREE SQL Restore and Shrink SQL DB Tools, and SQL
Read the Top 10 Books for Microsoft Developers, 15 Days FREE
Read the Top 10 Books for Microsoft Developers, 15 Days FREE
Try Safari Books Online - 15 Days FREE + 15% Off for 1 Year
Try Safari Books Online - 15 Days FREE + 15% Off for 1 Year
 
 Post a Feedback, Comment, or Question about this article
Subject:
Comment:
 Comments
ASP.Net 4 Hosting is here
 Hosted by MaximumASP  |  Found a broken link?  |  Contact Us  |  Terms & conditions  |  Privacy Policy  |  Site Map  |  Suggest an Idea  |  Media Kit
Current Version: 5.2010.8.14
 © 2010  contents copyright of their authors. Rest everything copyright Mindcracker. All rights reserved.