The ASPX Page and XSLT to style the XML from SQL Server (VB) : to XML « ADO.net Database « ASP.NET Tutorial






<%@ Page Language="VB" 
         AutoEventWireup="false" 
         CodeFile="Default.aspx.vb" 
         Inherits="_Default" %>
         
         
<asp:xml id="Xml1" runat="server" transformsource="~/Data.xslt"/>

File: Data.xslt

Imports System.Data
Imports System.Data.SqlClient
Imports System.IO
Imports System.Xml
Imports System.Xml.XPath
Imports System.Xml.Xsl

Partial Class _Default
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) _
           Handles Me.Load
        Dim connStr As String = "database=Northwind;Data Source=.\SQLEXPRESS;" & _
                " User id=Tom;pwd=password"
        Dim x As New XmlDocument()
        Dim xpathnav As XPathNavigator = x.CreateNavigator()
        Using conn As New SqlConnection(connStr)
            conn.Open()
            Dim command As New SqlCommand("select * from Customers as Customer " & _
                "for XML AUTO, ELEMENTS", conn)
            Using xw As XmlWriter = xpathnav.PrependChild()
                xw.WriteStartElement("Customers")
                Using xr As XmlReader = command.ExecuteXmlReader()
                    xw.WriteNode(xr, True)
                End Using
                xw.WriteEndElement()
            End Using
        End Using
        Xml1.XPathNavigator = xpathnav
    End Sub
End Class

File: Data.xslt

<?xml version="1.0" encoding="utf-8" ?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:output method="html"/>
    <xsl:template match="/">
        <h3>List of Customers</h3>
        <table border="1">
            <tr>
                <td>Company Name</td><td>Contact Name</td><td>Contact Title</td>
            </tr>
            <xsl:apply-templates select="//Customer"/>
        </table>
    </xsl:template>
    <xsl:template match="Customer">
        <tr>
            <td><xsl:value-of select="CompanyName"/></td>
            <td><xsl:value-of select="ContactName"/></td>
            <td><xsl:value-of select="ContactTitle"/></td>
        </tr>
    </xsl:template>
</xsl:stylesheet>








18.56.to XML
18.56.1.XML query
18.56.2.The ASPX Page and XSLT to style the XML from SQL Server (VB)
18.56.3.The ASPX Page and XSLT to style the XML from SQL Server (C#)
18.56.4.Retrieving XML from SQL Server 2000 using FOR XML AUTO (C#)
18.56.5.Retrieving XML from SQL Server 2000 using FOR XML AUTO (VB)
18.56.6.Run a query against a SQL Server database with an XML typed column. It then displays results in a tree-view.