.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Code Snippets >> ASP.NET Controls >> Post New Resource Bookmark and Share   

 Subscribe to Code Snippets

Display xml file using xml control

Posted By:satyapriyanayak       Posted Date: March 11, 2013    Points: 40    Category: ASP.NET Controls    URL: http://www.dotnetspark.com  

Display xml file using xml control
 

Here 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

     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More code samples in C#, ASP.Net, Vb.Net and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend