Microsoft KB Archive/301244

= How To Return XML from an ASP Web Page =

Article ID: 301244

Article Last Modified on 6/29/2004

-

APPLIES TO


 * Microsoft Active Server Pages 4.0

-



This article was previously published under Q301244



SUMMARY
Returning XML data from an Active Server Pages (ASP) Web page is a common programming requirement. The method that is adopted to implement this requirement varies depending on the version of Microsoft Internet Information Services (IIS) that is used to host the ASP application. This document contains step-by-step instructions with relevant code samples to demonstrate how Extensible Markup Language (XML) data can be returned from an ASP page.

The code samples below create an ASP page that returns the contents of an ActiveX Data Objects (ADO) recordset that is persisted in XML format. The code connects to an instance of the SQL Server PUBS sample database, and opens an ADO recordset by executing a SELECT query to retrieve data from the Authors table. The recordset is then returned in XML format to the client browser by using ADO's persisting capabilities and the Write method of the ASP Response object.

1. Prerequisites
The following list outlines the recommended hardware, software, network infrastructure, and service packs that you will need:
 * Microsoft Windows 2000 Professional, Windows 2000 Server, or Windows 2000 Advanced Server
 * Microsoft Internet Information Server (IIS) 5.0

or


 * Microsoft Windows NT 4.0 Server
 * Microsoft Internet Information Server (IIS) 4.0

2. Prepare the Web Site
 In Windows Explorer, create a folder named Xmltest under your Web server's root folder, which is typically located at C:\Inetpub\Wwwroot\. Right-click the newly created folder, and then click Properties. On the Security tab, add the Everyone group, and allow both Read and Write permissions for the Everyone group to the folder. Click OK to accept the changes. On the Start menu, point to Programs, point to Administrative Tools, and then click Internet Services Manager. Under Internet Information Services, double-click to expand the entry for the local server. Right-click the Default Web Site, point to New, and then click Virtual Directory. In the wizard, follow these steps:  When prompted, type XMLTest in the Virtual Directory Alias text box, and then click Next.</li> When you are prompted to type the Web Site Content Directory, click Browse, select the newly created XMLTest directory, and then click Next.</li> When you are prompted to select Access permissions, select Read and Run scripts (such as ASP). No other Access permissions are necessary for this example. Click Next to complete the wizard.</li></ol> </li> Double-click the Default Web Site.</li> Right-click the new virtual directory, and then click Properties.</li> On the Directory tab, verify that the Web site name (as typed in step 6a) is listed in the Application Name text box under Application Settings. If it is not, click Create to create the application.</li> Close the Properties dialog box and IIS.</li></ol>

3. Windows 2000 XML Sample Code
<ol> On the Start menu, point to Programs, point to Accessories, and then click Notepad.</li>  Select the following code, right-click the code, and then click Copy. In Notepad, click Paste on the Edit menu to add the following code to the file: <%

'Very Important : Set the ContentType property of the Response object to text/xml.

Response.ContentType = &quot;text/xml&quot;

Dim cn Dim rs Dim xmlDoc

Set cn=Server.CreateObject(&quot;ADODB.Connection&quot;) Set rs=Server.CreateObject(&quot;ADODB.Recordset&quot;)

'Replace the ADO Connection string attributes 'in the following line of code to point to your 'instance of SQL Server, and to specify the 'required security credentials for User ID and Password.

cn.Open &quot;Provider=SQLOLEDB.1;&quot; & _ &quot;User ID= ;&quot; & _ &quot;Password= ;&quot; & _ &quot;Initial Catalog=pubs;&quot; & _ &quot;Data Source= &quot;

rs.CursorLocation = 3 rs.Open &quot;Select * from Authors&quot;,cn

'Persist the Recorset in XML format to the ASP Response object. 'The constant value for adPersistXML is 1.

rs.Save Response, 1

%>                   </li> On line 20 of the code, replace   with your username.</li> On line 21 of the code, replace   with your password.</li> On line 23 of the code, replace   with your SQL Server.</li> On the File menu, click Save.</li> In the Save in drop-down list box, browse to the Xmltest folder that you created earlier. In the File name text box, type Xmlw2k.asp, and click All Files in the Save as Type drop-down list box. Finally, click Save to save the file.</li> To view the page, start your Web browser and type the HTTP location of the page in the address bar. If you saved the file in the previously mentioned location, type http://<servername>/Xmltest/Xmlw2k.asp in the address bar.</li></ol>

4. Windows NT 4.0 XML Sample Code
<ol> On the Start menu, point to Programs, point to Accessories, and then click Notepad.</li>  Select the following code, right-click the code, and then click Copy. In Notepad, click Paste on the Edit menu to add the following code to the file: <% 'Very Important : Set the ContentType property of 'the Response object to text/xml.

Response.ContentType = &quot;text/xml&quot;

Dim cn Dim rs Dim xmlDoc

Set cn=Server.CreateObject(&quot;ADODB.Connection&quot;) Set rs=Server.CreateObject(&quot;ADODB.Recordset&quot;)

'Replace the ADO Connection string attributes 'in the following line of code to point to your 'instance of SQL Server, and to specify the 'required security credentials for User ID and Password.

cn.Open &quot;Provider=SQLOLEDB.1;&quot; & _ &quot;User ID= ;&quot; & _ &quot;Password=<password;&quot; & _ &quot;Initial Catalog=pubs;&quot; & _ &quot;Data Source= &quot;

rs.CursorLocation = 3 rs.Open &quot;Select * from Authors&quot;,cn

Set xmlDoc = Server.CreateObject(&quot;Microsoft.XMLDOM&quot;)

'Persist the Recorset in XML format to the DOMDocument object. 'The constant value for adPersistXML is 1.

rs.Save xmlDoc,1

rs.Close cn.Close

Set rs = Nothing Set cn = Nothing

'Write out the xml property of the DOMDocument 'object to the client Browser Response.Write xmldoc.xml %>                   </li> <li>On line 20 of the code, replace   with your username.</li> <li>On line 21 of the code, replace   with your password.</li> <li>On line 23 of the code, replace   with your SQL Server.</li> <li>On the File menu, click Save.</li> <li>In the Save in drop-down list box, browse to the Xmltest folder that you created earlier. In the File name text box, type Xmlnt4.asp, and click All Files in the Save as Type drop-down list box. Finally, click Save to save the file.</li> <li>To view the page, start your Web browser, and type the HTTP location of the page in the address bar. If you saved the file in the above-mentioned location, type http://<servername>/Xmltest/Xmlnt4.asp in the address bar.NOTE: The Windows NT 4.0 sample code also works when you use IIS 5.0 with Windows 2000.</li></ol>

5. Pitfalls
<ol> <li> When you access the ASP page, the browser returns a blank page. Check to make sure that you have included the following line at the top of the ASP page: Response.ContentType = &quot;text/xml&quot; This should be the first line of code in the ASP page. </li> <li>In IIS 5.0 (ASP 3.0), the ASP Response object implements the IStream interface. It is therefore possible to directly persist an ADO recordset (in XML format), or an MSXML DOMDocument object loaded with XML data to the Response object.</li> <li>In IIS 4.0 (ASP 2.0), the ASP Response object does not implement the IStream COM interface. It is therefore necessary to use the Write method of the Response object to return XML from the ASP. The argument to the Write method must be a well-formed XML string, or a variable that contains a well-formed XML string.</li></ol>

Additional query words: kbVidXML

Keywords: kbhowto kbhowtomaster kbscript kbdatabase kbaspobj KB301244

-

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

© Microsoft Corporation. All rights reserved.