ARTICLE

Display xml file using xml control in VB.NET

Posted by Satyapriya Nayak Articles | ASP.NET using VB.NET August 29, 2011
In this article we will know how to Display xml file using xml control.
Download Files:
 
Reader Level:

In this article we will know how to Display xml file using xml control.

For that we have to add an Xml control, xml file and XSLTFile.

First add a xml file to the webform and write the below code to that file and name that file as

Student.xml

<?xml version="1.0" encoding="utf-8" ?>

<studentdetails>

<student>

<sid>s001</sid>

<sname>Raj</sname>

<smarks>100</smarks>

<saddress>Delhi</saddress>

</student>

<student>

<sid>s002</sid>

<sname>Ravi</sname>

<smarks>90</smarks>

<saddress>Mumbai</saddress>

</student>

<student>

<sid>s003</sid>

<sname>Rahul</sname>

<smarks>80</smarks>

<saddress>Pune</saddress>

</student>

</studentdetails>

Then add an Xslt file to the webform. To display the student.xml file in tabular form, which will, appears in the webform, we write the below code in this Xslt file.

Student. Xslt

<?xml version="1.0" encoding="utf-8" ?>

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

  <xsl:template match="student">

    <xsl:value-of select="sid" />

    <xsl:value-of select="smarks" />

    <xsl:if test="position() != last() ">,</xsl:if>

  </xsl:template>

  <xsl:template match="/">

    <HTML>

      <head>

        <title>

          StudentDetails

        </title>

      </head>

      <body>

        <table border="10">

          <tr bgcolor="silver">

            <th>sid</th>

            <th>sname</th>

            <th>smarks</th>

            <th>saddress</th>

          </tr>

          <xsl:for-each select="studentdetails/student">

            <tr>

              <td>

                <xsl:value-of select="sid" />

              </td>

              <td>

                <xsl:value-of select="sname" />

              </td>

              <td>

                <xsl:value-of select="smarks" />

              </td>

              <td>

                <xsl:value-of select="saddress" />

              </td>

              <td>

                <xsl:apply-templates  select="student" />

              </td>

            </tr>

          </xsl:for-each>

        </table>

      </body>

    </HTML>

  </xsl:template>

</xsl:stylesheet>

Default.aspx code

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head runat="server">

    <title>Untitled Page</title>

</head>

<body>

    <form id="form1" runat="server">

    <div>  

    </div>

    <asp:Xml ID="Xml1" runat="server"></asp:Xml>

    </form>

</body>

</html>

Default.aspx.vb code

Imports System.Data

Imports System.Xml

Partial Class _Default

    Inherits System.Web.UI.Page 

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        If Not IsPostBack Then

            Dim ds As New DataSet

            ds.ReadXml(MapPath("student.xml"))

            Xml1.Document = New XmlDataDocument(ds)

            Xml1.TransformSource = "student.xslt"

        End If

    End Sub

End Class

Output

xml1.gif

Thanks for reading

Login to add your contents and source code to this article
share this article :
post comment
 
Team Foundation Server Hosting
Become a Sponsor
PREMIUM SPONSORS
  • 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.
    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.
Team Foundation Server Hosting
Become a Sponsor