<script type=text/javascript>loadTOCNode(1, 'moreinformation');</script>
要将控件 绑定到Web页上的XML, 必须首先在页声明 XML 数据源。 此源可以是服务器上的 XML 文件。 绑定到 XML 数据的更有用方法是: 使用 网 页以编程方式生成 XML 并绑定到该 网页。 代码示例演示了两种绑定到 XML 数据的方法。
第一种方法绑定直接到输出通过遍历记录并生成 XML 标记为每个记录和字段手动生成 XML 文件。
第二种方法, 仍然存在记录直接写入响应流作为 XML, 和需要服务器端代码中少工作但需要一些其他操作客户端上的数据。 对于本示例能够正常, 服务器上必须安装 Northwind 数据库并且一个 ODBC 数据源名称 (DSN) 必须存在命名 Northwind, 它指向Northwind数据库。
1. | 首先, 将以下文本插入文件名为 Sample.asp 并文件保存到您 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 %>
此文件记录的文本保存到 XML, 或直 XML 中记录集的内容输出由循环和添加标记为每个字段, 取决于是否 RS QueryString 值设置为 1 生成输出。
|
2. | 插入下列代码到名为 Sample.htm 文件并保存到与步骤 1 中 Sample.asp 相同位置 Sample.htm 文件: <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>
|
Sample.htm 文件从两种格式中 Sample.asp 页检索 XML 数据。 第一节绑定到中手动创建 XML 数据。 绑第二部分从持久化记录, 使用 adPersistXML 选项, 需要一些说明定到 XML 数据。
如果您检索的永久性记录值通过导航至 Sample.asp?RS=1 并查看源文件的输出, 您将注意输出的结构是不简单 XML 标记为每个字段。 XML 数据是像分层记录比标准记录, 因为它可嵌套到任意深度。 持久化 XML 记录格式使用此嵌套来首先描述字段, 和然后呈现数据 rs:data 部分中。 每行由 z:row 元素, 其属性是值的字段对于该行。 如果记录是分层, 然后了其他记录字段值将嵌套作为输出中其他元素。
由于有, 表代表记录数据必须嵌套在另一个表。 表用作 ID 为同一 XML 数据岛 DATASRC 属性。 但是, DATAFLD 值进行表已绑定到 rs:data 部分, 而且 z:row 节绑定的内部表 DATAFLD 值。 绑表中跨越然后定到实际字段名。 复杂程度客户上此方法备份是对由简化与服务器上生成输出。 通过将 Response 对象的 IStream 接口用作目标和 adPersistXML 常量作为输出格式调用 rs.Save 方法。