How To Return XML from an ASP Web Page

Exonerare de răspundere pentru conținutul retras din baza de cunoștințe

Acest articol a fost scris despre produsele pentru care Microsoft nu mai oferă asistență. Prin urmare, acest articol este oferit „ca atare” și nu mai va fi actualizat.

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

  1. In Windows Explorer, create a folder named Xmltest under your Web server's root folder, which is typically located at C:\Inetpub\Wwwroot\.
  2. Right-click the newly created folder, and then click Properties.
  3. 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.
  4. On the Start menu, point to Programs, point to Administrative Tools, and then click Internet Services Manager.
  5. Under Internet Information Services, double-click to expand the entry for the local server.
  6. Right-click the Default Web Site, point to New, and then click Virtual Directory. In the wizard, follow these steps:
    1. When prompted, type XMLTest in the Virtual Directory Alias text box, and then click Next.
    2. When you are prompted to type the Web Site Content Directory, click Browse, select the newly created XMLTest directory, and then click Next.
    3. 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.
  7. Double-click the Default Web Site.
  8. Right-click the new virtual directory, and then click Properties.
  9. 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.
  10. Close the Properties dialog box and IIS.

3. Windows 2000 XML Sample Code

  1. On the Start menu, point to Programs, point to Accessories, and then click Notepad.
  2. 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 = "text/xml"

    Dim cn
    Dim rs
    Dim xmlDoc

    Set cn=Server.CreateObject("ADODB.Connection")
    Set rs=Server.CreateObject("ADODB.Recordset")

    '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 "Provider=SQLOLEDB.1;" & _
    "User ID=<userid>;" & _
    "Password=<password>;" & _
    "Initial Catalog=pubs;" & _
    "Data Source=<servername>"

    rs.CursorLocation = 3
    rs.Open "Select * from Authors",cn

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

    rs.Save Response, 1

    %>
  3. On line 20 of the code, replace <userid> with your username.
  4. On line 21 of the code, replace <password> with your password.
  5. On line 23 of the code, replace <servername> with your SQL Server.
  6. On the File menu, click Save.
  7. 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.
  8. 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.

4. Windows NT 4.0 XML Sample Code

  1. On the Start menu, point to Programs, point to Accessories, and then click Notepad.
  2. 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 = "text/xml"

    Dim cn
    Dim rs
    Dim xmlDoc

    Set cn=Server.CreateObject("ADODB.Connection")
    Set rs=Server.CreateObject("ADODB.Recordset")

    '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 "Provider=SQLOLEDB.1;" & _
    "User ID=<userid>;" & _
    "Password=<password;" & _
    "Initial Catalog=pubs;" & _
    "Data Source=<servername>"

    rs.CursorLocation = 3
    rs.Open "Select * from Authors",cn

    Set xmlDoc = Server.CreateObject("Microsoft.XMLDOM")

    '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
    %>
  3. On line 20 of the code, replace <userid> with your username.
  4. On line 21 of the code, replace <password> with your password.
  5. On line 23 of the code, replace <servername> with your SQL Server.
  6. On the File menu, click Save.
  7. 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.
  8. 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.

5. Pitfalls

  1. 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 = "text/xml"
    This should be the first line of code in the ASP page.
  2. 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.
  3. 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.
Proprietăți

ID articol: 301244 - Ultima examinare: 29 iun. 2004 - Revizie: 1

Feedback