Notice: This website is an unofficial Microsoft Knowledge Base (hereinafter KB) archive and is intended to provide a reliable access to deleted content from Microsoft KB. All KB articles are owned by Microsoft Corporation. Read full disclaimer for more details.

How To Bind to XML Data with Internet Explorer


View products that this article applies to.

This article was previously published under Q258295

↑ Back to the top


Summary

It is possible to bind fields on a Web page directly to an XML data source included in the page as a data island. An ASP page can generate XML, and serve as the source of data for an XML data island included in an ASP page. It is also possible to persist an ActiveX Data Objects (ADO) recordset to a stream as XML. Because the Response object in Active Server Pages (ASP) supports the IStream interface (Internet Information Server 5.0 only), an ASP page can pose as XML, and serve as the source of an XML data island. This article discusses two approaches for binding to data on a Web page to XML.

↑ Back to the top


More information

To bind to XML on a Web page, a reference must first be made on the page to the XML data source. This source can be an XML file on a server. A more useful means of binding to XML data is to use an ASP page to generate XML programmatically and bind to that ASP page. This method provides flexibility in the content that is delivered, as the ASP page can output the data in whatever format is necessary. The code sample that follows demonstrates two ways of binding to XML data.

The first method binds directly to a manually generated XML file that is output by looping through a recordset and generates XML tags for each record and field.

The second method, persists the recordset directly to the Response stream as XML, and requires less work in the server side code but requires some additional manipulation of the data on the client. For this sample to work properly, the Northwind database must be installed on the server, and an ODBC data source name (DSN) must exist named Northwind, which points to the Northwind database.

  1. First, insert the following text into a file named Sample.asp and save the file to a location on your WebServer:
    <%@ Language=VBScript %>
    <%
    
    	const adPersistXML = 1
    
    	dim rs, cn
    	
    	Response.ContentType = "text/xml"
    	
    	set rs = Server.CreateObject("ADODB.Recordset")
    	set cn = Server.CreateObject("ADODB.Connection")
    	cn.Open "DSN=Northwind;UID=sa;PWD=;"
    	rs.Open "SELECT CustomerID, CompanyName, ContactName, Country FROM Customers WHERE CustomerID < 'C'", cn 
    	if Request.QueryString("RS")="1" then
    		rs.Save Response, adPersistXML
    	else
    		dim x
    		Response.Write "<?xml version=""1.0""?>"
    		Response.Write "<DATA>"
    		while not rs.EOF
    			Response.Write "<RECORD>"
    			for x = 0 to rs.Fields.Count - 1
    				Response.Write "<" + rs.Fields(x).Name + ">"
    				Response.Write Server.HTMLEncode(trim(cstr(rs.Fields(x).Value)))
    				Response.Write "</" + rs.Fields(x).Name + ">"
    			next
    			Response.Write "</RECORD>"
    			rs.MoveNext 
    		wend
    		Response.Write "</DATA>"
    	end if
    	rs.Close
    	cn.Close
    	set rs = nothing
    	set cn = nothing
    %>
    					
    This file outputs either the text of a recordset persisted to XML, or the contents of the recordset in straight XML output that is generated by looping through and adding tags for each field, depending on whether the QueryString value RS is set to 1.

  2. Insert the following code into a file named Sample.htm and save the Sample.htm file to the same location as the Sample.asp in step 1:
    <HTML>
    <HEAD>
    <META NAME="GENERATOR" Content="Microsoft Visual Studio 6.0">
    <TITLE>XML Data Binding Sample</TITLE>
    </HEAD>
    <SCRIPT LANGUAGE="VBScript">
    	Sub cmdData1_OnClick
    		if Data1.style.display = "" then
    			Data1.style.display = "none"
    			cmdData1.value = "Hide table bound to straight XML."
    		else
    			Data1.style.display = ""
    			cmdData1.value = "Click to see table bound to straight XML."
    		end if
    	End Sub
    	
    	Sub cmdData2_OnClick
    		if Data2.style.display = "" then
    			Data2.style.display = "none"
    			cmdData2.value = "Hide table bound to a persisted recordset."
    		else
    			Data2.style.display = ""
    			cmdData2.value = "Click to see table bound to a persisted recordset."
    		end if
    	End Sub
    </SCRIPT>
    <BODY>
    <xml id="PersistedRS" src="Sample.asp?RS=1"></xml>
    <xml id="StraightXML" src="Sample.asp"></xml>
    <input type=button name="cmdData1" value="Click to see table bound to straight XML." style="width:300;"><br><br>
    <table id=Data1 datasrc="#StraightXML" border=1 align=center style="display:none;">
    	<thead>
    		<tr>
    			<th colspan=11>This table is bound directly to an XML data source.</th>
    		</tr>
    		<tr>
    			<th>Customer ID</th>
    			<th>Company Name</th>
    			<th>Contact Name</th>
    			<th>Country</th>
    		</tr>
    	</thead>
    	<tr>
    		<td><span datafld="CustomerID"></span></td>
    		<td><span datafld="CompanyName"></span></td>
    		<td><span datafld="ContactName"></span></td>
    		<td><span datafld="Country"></span></td>
    	</tr>
    </table>
    <br><br>
    <input type=button name="cmdData2" value="Click to see table bound to a persisted recordset." style="width:300;"><br><br>
    <table  id=Data2 datasrc="#PersistedRS" datafld="rs:data" style="display:none;" align=center cellspacing=0 cellpadding=0>
    	<tr><td>
    		<table datasrc="#PersistedRS" datafld="z:row" border=1 align=center>
    			<thead>
    				<tr>
    					<th colspan=11>This table is bound directly to an XML data source.</th>
    				</tr>
    				<tr>
    					<th>Customer ID</th>
    					<th>Company Name</th>
    					<th>Contact Name</th>
    					<th>Country</th>
    				</tr>
    			</thead>
    			<tr>
    				<td><span datafld="CustomerID"></span></td>
    				<td><span datafld="CompanyName"></span></td>
    				<td><span datafld="ContactName"></span></td>
    				<td><span datafld="Country"></span></td>
    			</tr>
    		</table>
    	</td></tr>
    </table>
    </BODY>
    </HTML>
    					
The Sample.htm file retrieves the XML data from the Sample.asp page in both formats. The first section binds to the data in the XML that is manually created. The second section binds to the XML data from the persisted recordset, using the adPersistXML option, which requires some explanation.

If you retrieve the value of the persisted recordset by navigating to Sample.asp?RS=1 and you view the source of the output, you will notice that the structure of the output is not simple XML tags for each field. XML data is more like hierarchical recordsets than standard recordsets, because it can be nested to any depth. The persisted XML recordset format uses this nesting to first describe the fields, and then present the data in the rs:data section. Each row is represented by a z:row element, whose attributes are the values of the fields for that row. If the recordset was hierarchical, then the field values that were other recordsets will be nested as additional elements within the output.

Because of this, the table that represents the recordset data must be nested within another table. Both tables use as their DATASRC property the id of the same XML data island. However, the DATAFLD value for the first table is bound to the rs:data section, and the DATAFLD value for the inner table is bound to the z:row section. The spans within the table are then bound to the actual field names. The complexity of this method on the client is made up for by the ease with which the output is generated on the server. You call the rs.Save method by using the Response object's IStream interface as the destination, and the adPersistXML constant as the output format.

↑ Back to the top


Keywords: KB258295, kbhowto

↑ Back to the top

Article Info
Article ID : 258295
Revision : 4
Created on : 6/29/2004
Published on : 6/29/2004
Exists online : False
Views : 576