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 id="Head1" 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) HandlesMe.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