Blue Theme Orange Theme Green Theme Red Theme
 
Team Foundation Server Hosting
Home | Forums | ASP.NET 2.0 Tutorials | Web Services | How Do I...? | Class Browser | WPF Quick Starts | Advertise with Us
 | Consulting  
Submit an Article Submit a Blog 
 Jump to
Skip Navigation Links
TechnologyExpand Technology
WebsiteExpand Website
Nevron Gauge for SharePoint
Search :       Advanced Search »
Home » XML in VB.NET » 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 :
Page Views : 9012
Downloads : 0
Rating :
 Rate it
Level : Beginner
   Print Read/Post comments Post a comment  Similar Articles  
   Email to a friend  Bookmark  Author's other articles  
 
Nevron Gauge for SharePoint
Become a Sponsor
 Tag Cloud
 Latest Jobs
More ... 
 Latest Interview Questions
More ... 


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. 

Comment Request!
Thank you for reading this post. Please post your feedback, question, or comments about this post Here.
Login to add your contents and source code to this article
 [Top] Rate this article
 
 About the author
 
Author
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.
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.
ASP.NET 4 Hosting
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!
 
 Post a Feedback, Comment, or Question about this article
Subject:
Comment:
Nevron Gauge for SharePoint
Become a Sponsor
 Comments
6 Months Free & No Setup Fees ASP.NET Hosting!
 © 2012  contents copyright of their authors. Rest everything copyright Mindcracker. All rights reserved.