Retrieving XML from SQL Server 2000 using FOR XML AUTO (VB) : to XML « ADO.net Database « ASP.NET Tutorial






<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>

File: Default.aspx.vb

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
        Response.ContentType = "text/xml"
        x.Save(Response.Output)

    End Sub

End Class








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.