Click here to Skip to main content
15,879,239 members
Articles / Web Development / ASP.NET
Article

ASP.NET 2.0 -- Standard Controls -- XML Control

Rate me:
Please Sign up or sign in to vote.
3.76/5 (8 votes)
8 Sep 20062 min read 52.8K   26   5
ASP.NET 2.0 -- Standard Controls -- XML Control

Introduction

Today we will discuss the XML Control added in ASP.NET 2.0 standard controls library.

Over the past few years storing data in XML has become very popular. Data stored in XML can be read using a lot of inbuilt objects provided by .NET framework like XMLReader, XPath Query and the old fashioned XMLDOM object. In ASP.NET MS has gone a step ahead and provided an XML control which easily reads as well as transforms your data in the desired format using xslt.

Earlier you would have created an XML DOM object, loaded the xml file in DOM, Traveresed through the various nodes and then written a function that would format the data in the desired format and diplsay it on the page. The same can be performed using XMLReader and XPath Queries.

XML Control uses the power of xslt. Personally I have been a great fan of XSLT. The reason - XSLT is a scripting language for transforming XML .It has condiions , loops.etc which allow you to parse through the xml and reformat or even recreate an completely different xml using the same data.

XML Control Sample Application

Let us now create a sample and check out the power of xslt on XML transformation.

1. Open a new web project,Add a new form and add drag and drop the XML control on the form.

2. Second to the project add an xml file called Employee.xml and add the following content to the xml file

XML
<?xml version="1.0" encoding="utf-8" ?>
<employees>
   <employee>
          <name>Namrata Shah</name>
          <id>3456</id>
          <tele>123-123-2343</tele>
          <email>ns@mphasis.com</email>  
  </employee>
     <employee>
          <name>Tony Blair</name>
          <id>4567</id>
           <tele>234-644-8904</tele>
           <email>tb@uk.com</email>   
  </employee>
      <employee>
            <name>George Bush</name>
            <id>5678</id>
            <tele>123-423-2343</tele>
           <email>gb@usa.com</email>    
  </employee>
</employees>

3. Add an Employee.xslt file to the project and add the following content
XML
<?xml version="1.0" encoding="utf-8"?> 
<xsl:stylesheet xmlns:xsl="<A href="http://www.w3.org/1999/XSL/Transform">http://www.w3.org/1999/XSL/Transform</A>" version="1.0">
  <xsl:output method="html"/> 
  <xsl:template match="/">
    <html>
      <head>
        <title>ABC Employee Register</title>
      </head>
      <body>
        <xsl:apply-templates />
      </body>
    </html>
  </xsl:template> 
  <xsl:template match="employees">
    <h4>ABC Group of Companies</h4><hr color="white"/>
    <xsl:apply-templates />
  </xsl:template>
  <xsl:template match="id">
    <b>Emp ID :</b> <xsl:apply-templates />
    <br/>
  </xsl:template>
  <xsl:template match="name">
    <b>Name:</b> <xsl:apply-templates /><br/>
  </xsl:template>
  <xsl:template match="tele">
    <b>Phone:</b> <xsl:apply-templates /><br/>
  </xsl:template>
  <xsl:template match="email">
    <b>Email:</b> <xsl:apply-templates /><br/>  <br/>
    <br/>
  </xsl:template>
</xsl:stylesheet>

4. The xml file has data for the employees of ABC Group of Companies and we need to create a screen which displays this information on the web page.

5. Let us set the values of DocumentSource of the XML object to Employee.xml. AFter setting the document source  execute the project and observe the results on the screen.

6. You should see the list of all the records present in the XML file as follows

<P lang=text>Namrata <A href="mailto:Shah3456123-123-2343ns@mphasis.comTony">Shah3456123-123-2343ns@mphasis.comTony</A> <A href="mailto:Blair4567234-644-8904tb@uk.comGeorge">Blair4567234-644-</A> 
<P lang=text><A href="mailto:8904tb@uk.comGeorge">8904tb@uk.comGeorge</A> <A href="mailto:Bush5678123-423-2343gb@usa.com">Bush5678123-423-2343gb@usa.com</A></P>


7. The records a shown in flat style one after the other. Definitely the user would not want to view the records in a liner format as above. WE need to display the records to the user in a tabular format separating the records.

8. Now let us set the value for Transformsource. Select the Employee.xslt we created.

9. Compile and build and execute the project snf you will view a tabulr record by record view of all the employees working for ABC corp.

 

Sample screenshot

 

10. XSLT has transformed the Employee data present in employee.xml it not more readable and meaningful format.

Thanks a lot.

Regards,
Namratha (Nasha)

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Architect
United States United States
Namratha Shah a.k.a. Nasha is from orginally from Bombay, India but currently residing NJ, USA. She has to her credit, a Bachelor’s Degree in Microbiology and Biotechnology and a Master's in Computer and Software Applications (1999-2001) from Somaiya College Bombay. She started her career with C and C++ and then moved on to Microsoft Technologies. She has over 7.5 years experience in software architecture, design and development. She is a Certified Scrum Master and a member of the CORE .NET Architecture team. She has been Awarded with Microsoft’s Prestigious Most Valuable Professional (MVP) twice consecutively in years 2005 and 2006 in Visual C#.NET for her outstanding contributions to the .NET community.

Comments and Discussions

 
GeneralMy vote of 5 Pin
abhishekpattali11-Nov-11 0:29
abhishekpattali11-Nov-11 0:29 
GeneralMy vote of 5 Pin
adirock28-Sep-10 21:06
adirock28-Sep-10 21:06 
Generalpaging Pin
imperialx29-Jun-07 5:02
imperialx29-Jun-07 5:02 
Generalneeds cleanup Pin
nd212-Sep-06 13:44
nd212-Sep-06 13:44 
GeneralThanks for article. Pin
vik2010-Sep-06 20:08
vik2010-Sep-06 20:08 
Can you add more detail on how to work with XSLT

vikram
http://www.vikramlakhotia.com/

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.