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
  1. <?xml version="1.0" encoding="utf-8" ?>
  2. <studentdetails>
  3. <student>
  4. <sid>s001</sid>
  5. <sname>Raj</sname>
  6. <smarks>100</smarks>
  7. <saddress>Delhi</saddress>
  8. </student>
  9. <student>
  10. <sid>s002</sid>
  11. <sname>Ravi</sname>
  12. <smarks>90</smarks>
  13. <saddress>Mumbai</saddress>
  14. </student>
  15. <student>
  16. <sid>s003</sid>
  17. <sname>Rahul</sname>
  18. <smarks>80</smarks>
  19. <saddress>Pune</saddress>
  20. </student>
  21. </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

  1. <?xml version="1.0" encoding="utf-8" ?>
  2. <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  3. <xsl:template match="student">
  4. <xsl:value-of select="sid" />
  5. <xsl:value-of select="smarks" />
  6. <xsl:if test="position() != last() ">,</xsl:if>
  7. </xsl:template>
  8. <xsl:template match="/">
  9. <html>
  10. <head>
  11. <title>StudentDetails </title>
  12. </head>
  13. <body>
  14. <table border="10">
  15. <tr bgcolor="silver">
  16. <th>
  17. sid
  18. </th>
  19. <th>
  20. sname
  21. </th>
  22. <th>
  23. smarks
  24. </th>
  25. <th>
  26. saddress
  27. </th>
  28. </tr>
  29. <xsl:for-each select="studentdetails/student">
  30. <tr>
  31. <td>
  32. <xsl:value-of select="sid" />
  33. </td>
  34. <td>
  35. <xsl:value-of select="sname" />
  36. </td>
  37. <td>
  38. <xsl:value-of select="smarks" />
  39. </td>
  40. <td>
  41. <xsl:value-of select="saddress" />
  42. </td>
  43. <td>
  44. <xsl:apply-templates select="student" />
  45. </td>
  46. </tr>
  47. </xsl:for-each>
  48. </table>
  49. </body>
  50. </html>
  51. </xsl:template> </xsl:stylesheet>
Default.aspx code
  1. <%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  3. <html xmlns="http://www.w3.org/1999/xhtml">
  4. <head id="Head1" runat="server">
  5. <title>Untitled Page</title>
  6. </head>
  7. <body> <form id="form1" runat="server">
  8. <div
  9. </div>
  10. <asp:Xml ID="Xml1" runat="server"></asp:Xml>
  11. </form>
  12. </body>
  13. </html>
Default.aspx.vb code
  1. Imports System.Data
  2. Imports System.Xml
  3. Partial Class _Default
  4. Inherits System.Web.UI.Page
  5. Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) HandlesMe.Load
  6. If Not IsPostBack Then
  7. Dim ds As New DataSet
  8. ds.ReadXml(MapPath("student.xml"))
  9. Xml1.Document = New XmlDataDocument(ds)
  10. Xml1.TransformSource = "student.xslt"
  11. End If
  12. End Sub
  13. End Class
Output

xml.gif