How To Use XML Control In ASP.NET Using VB.NET

In this article you will learn that how can you use the XML control to display data in tabular format.
  • 2517
Download Files:
 

Introduction
 
XML control is used to write out an XML document or the Result of an XSL Transform. The Document source specify the XML document to use. This Document will be written directly to the output stream unless TransformSource is also Specified. TransformSource must be a valid XSL Transform document and will be used to Transform the XML document before its contents are written to the output stream. Here we are discussing an example in which we are creating an application to store the details of the book available for sale in the XML format. As we give the name to the website WebShoppe for the application. The details of the book will be displayed in a Tabular format in the Browser. The details include Book id, Title, Price, FirstName and LastName of the Author.

Getting Started

  • Simply create a new ASP.NET web application. 
  • Drag a Xml control on your page. Your page will look like below.

    xml1.gif
     
  • You can also add control by given code.

    <%@ Page Title="Home Page" Language="vb"  AutoEventWireup="false"
        CodeBehind="Default.aspx.vb" Inherits="xml._Default" %>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
    <HTML>
           <HEAD>
                  <title>WebForm1</title>
                  <meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
                  <meta content="vb" name="CODE_LANGUAGE">
                  <meta content="JavaScript" name="vs_defaultClientScript">
                  <meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema">
           </HEAD>
           <body ms_positioning="GridView">
                  <form id="Form1" method="post" runat="server">
                         <asp:Xml id="Xml1" runat="server" DocumentSource="Books.xml" TransformSource="Books.xslt"></asp:Xml>
            </form>
           </body>
    </HTML>
     
  • Then right-click on the project in solution explorer, click add new item than select a XML file give name Books.xml to it and click add. Your solution explorer will look like below.

    xml2.gif
     
  • Then add the below code in Books.xml file.

    <?xml version="1.0" encoding="UTF-8" ?>
    <Books>
      <
    Book>
        <
    id>1</id>
      <Title> Understanding XML </Title>
      <Price> $30 </Price>
      <author>
        <
    FirstName> Lily </FirstName>
        <LastName>
          Hicks
        </LastName>
      </
    author>   
      </
    Book>
      <
    Book >
        <
    id>2</id>
        <Title> .NET Framework </Title>
        <Price> $45 </Price>
        <author>
          <
    FirstName> Jasmine </FirstName>
          <LastName>
            Williams
          </LastName>
        </
    author>
      </
    Book>
    </
    Books>
     
  • Then again right-click and with the same above process add a XSLT file and give name Books.xslt to it. After adding the file your solution explorer will look like below.

    xml3.gif
     
  • Now add the below code in Books.xslt file.

    <?xml version="1.0" encoding="utf-8" ?>
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="Author">
           <xsl:value-of select="FirstName"/>
           <xsl:value-of select="LastName"/>
           <xsl:if test="position()!=last()">,</xsl:if>
    </xsl:template>
    <
    xsl:template match="/">
           <HTML>
           <
    HEAD><TITLE>BOOKS AT WEBSHOPPE</TITLE></HEAD
           <BODY>
           <
    H1>BOOKS AT WEBSHOPPE</H1>
           <TABLE BORDER="3" CELLSPACING="2" CELLPADDING="6">
           <THEAD ALIGN="CENTER" BGCOLOR="SILVER">
                  <TH> BOOK ID </TH>
                  <TH> TITLE </TH>
                  <TH> PRICE </TH>
                  <TH> AUTHOR(S) </TH>
           </THEAD>
           <
    TBODY>
                  <
    xsl:for-each select="Books/Book">
                  <TR>
                         <
    TD><font color="green">
                         <xsl:value-of select="id"/></font></TD>
                         <TD><xsl:value-of select="Title"/></TD>
                         <TD><xsl:value-of select="Price"/></TD>
                         <TD><xsl:apply-templates select="author"/></TD>
                  </TR>
                  </
    xsl:for-each>
           </
    TBODY>
           </
    TABLE>
           </
    BODY>
           </
    HTML>
    </
    xsl:template>
    </
    xsl:stylesheet>
     
  • Now run your application.

Output

xml4.gif

Summary
 
In this article you learned that how can you use the Xml control.

Categories

More Articles

© 2019 DotNetHeaven. All rights reserved.