Display XML File using XML Control in VB.NET

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

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.    
  10.   
  11. <html>  
  12. <head>  
  13.     <title>StudentDetails </title>  
  14. </head>  
  15. <body>  
  16.     <table border="10">  
  17.         <tr bgcolor="silver">  
  18.             <th>  
  19.                 sid  
  20.             </th>  
  21.             <th>  
  22.                 sname  
  23.             </th>  
  24.             <th>  
  25.                 smarks  
  26.             </th>  
  27.             <th>  
  28.                 saddress  
  29.             </th>  
  30.         </tr>  
  31.         <xsl:for-each select="studentdetails/student">  
  32.             <tr>  
  33.               <td>  
  34.                 <xsl:value-of select="sid" />  
  35.               </td>  
  36.               <td>  
  37.                 <xsl:value-of select="sname" />  
  38.               </td>  
  39.               <td>  
  40.                 <xsl:value-of select="smarks" />  
  41.               </td>  
  42.               <td>  
  43.                 <xsl:value-of select="saddress" />  
  44.               </td>  
  45.               <td>  
  46.                 <xsl:apply-templates  select="student" />  
  47.               </td>  
  48.             </tr>  
  49.           </xsl:for-each>  
  50.     </table>  
  51. </body>  
  52. </html>  
  53. </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 ObjectByVal 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