Microsoft KB Archive/258295

= How To Bind to XML Data with Internet Explorer =

Article ID: 258295

Article Last Modified on 6/29/2004

-

APPLIES TO


 * Microsoft XML Parser 2.0
 * Microsoft XML Core Services 4.0
 * Microsoft XML Core Services 4.0
 * Microsoft ActiveX Data Objects 2.5
 * Microsoft ActiveX Data Objects 2.6
 * Microsoft ActiveX Data Objects 2.7
 * Microsoft Internet Explorer 5.0
 * Microsoft Internet Explorer 5.01
 * Microsoft Internet Explorer 5.5
 * Microsoft Active Server Pages 4.0

-



This article was previously published under Q258295



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.



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.

  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 "" Response.Write " " 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.

  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:    XML Data Binding Sample   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  <BODY> <xml id="PersistedRS" src="Sample.asp?RS=1"> <xml id="StraightXML" src="Sample.asp"> <input type=button name="cmdData1" value="Click to see table bound to straight XML." style="width:300;"> <input type=button name="cmdData2" value="Click to see table bound to a persisted recordset." style="width:300;"> </BODY> </HTML> </li></ol>

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.

Keywords: kbhowto KB258295

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.