简体   繁体   English

如何使用SP查询Xml文档并从背后隐藏代码填充ASP DropDownList?

[英]How to query an Xml Document using a SP and populate an ASP DropDownList from code-behind?

Given the xml: 鉴于xml:

<?xml version="1.0" encoding="UTF-8"?>
<root>
    <lfSpec1ForLoc>
        <a href="/spec.aspx?id=20" title="AI">AI</a>
    </lfSpec1ForLoc>
    <lfSpec2ForLoc />
    <lfSpec3ForLoc />
    <lfSpec4ForLoc />
    <lfSpec5ForLoc />
    <lfSpec6ForLoc />
    <lfSpec7ForLoc />
    <lfSpec8ForLoc />
    <lfSpec9ForLoc />
    <lfSpec10ForLoc />
</root>

My URL is: www.mypage.com/off.aspx?id=12 我的网址是: www.mypage.com/off.aspx?id=12

I have the following Stored Procedure: 我有以下存储过程:

ALTER PROCEDURE [dbo].[MySP]
(
@LstrID varchar(200), -- 12 {from the query string in the above URL}
)

AS

BEGIN
    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.
    SET NOCOUNT ON;

    SELECT
        CAST ([con] AS XML).query('/root/lfSpec1ForLoc/a') AS [Spec1]
        , CAST ([con] AS XML).query('/root/lfSpec2ForLoc/a') AS [Spec2]
        , CAST ([con] AS XML).query('/root/lfSpec3ForLoc/a') AS [Spec3]
        , CAST ([con] AS XML).query('/root/lfSpec4ForLoc/a') AS [Spec4]
        , CAST ([con] AS XML).query('/root/lfSpec5ForLoc/a') AS [Spec5]
        , CAST ([con] AS XML).query('/root/lfSpec6ForLoc/a') AS [Spec6]
        , CAST ([con] AS XML).query('/root/lfSpec7ForLoc/a') AS [Spec7]
        , CAST ([con] AS XML).query('/root/lfSpec8ForLoc/a') AS [Spec8]
        , CAST ([con] AS XML).query('/root/lfSpec9ForLoc/a') AS [Spec9]
        , CAST ([con] AS XML).query('/root/lfSpec10ForLoc/a') AS [Spec10]
    FROM
        [myDB1].[dbo].[con]
    WHERE
        [folder_id] = 106
        AND 
        [content_id] = '%' + Lstr + '%'
END

The result from the SP is: SP的结果是:

Spec1                                           Spec2                                       Spec3   Spec4   Spec5   Spec6   Spec7   Spec8   Spec9   Spec10
<a href="/spe.aspx?id=1" title="AI">AI</a>      <a href="/spe.aspx?id=5" title="QW">QW</a>

I would like to populate the below dropdownlist: 我想填充以下下拉列表:

<ASP:DropDownList runat="server" ID="ddl1"></ASP:DropDownList>

So the HTML output is as follow: 因此,HTML输出如下:

<SELECT>
    <option value="/spe.aspx?id=1">AI</option>
    <option value="/spe.aspx?id=5">QW</option>
</SELECT>

What is the best way to achieve the populating the dropdownlist after executing the SP? 执行SP之后,实现填充下拉列表的最佳方法是什么?

I would change your PROC to make better usage of Xquery and the xml parsing capabilities of Sql Server. 我将更改您的PROC ,以更好地利用Xquery和Sql Server的xml解析功能。

What you can do from your PROC is project a table of (URL, Title) pairs from your the xml Records in your table with a query as simple as: 您可以通过PROC进行的操作是,通过查询中的XML记录,从表中的xml记录中构建一个(URL, Title)对表。

SELECT Nodes.node.value('(a/@href)[1]', 'varchar(100)') AS URL,
       Nodes.node.value('(a/@title)[1]', 'varchar(100)') AS Title
FROM 
(
  SELECT CAST(con.con AS XML) AS con,
    folder_id,
    content_id
    FROM con
) x
CROSS APPLY x.Con.nodes('/root/*[a]') as Nodes(node)
WHERE
[folder_id] = 106
AND 
[content_id] LIKE '%' + @LstrID + '%';

The /root/*[a] will navigate all lfSpecxForLoc nodes (irrespective of name), and filter just those with an a child element, thus avoiding the need to filter out nulls. /root/*[a]将导航所有lfSpecxForLoc节点(不论名称),和过滤器只是那些与a子元素,从而避免了需要过滤掉空值。 Also, by projecting the elements as individual rows, you won't need to scrape out 10 explicit columns - there will be as many rows as there are data. 另外,通过将元素投影为单独的行,您将无需刮除10个显式的列-行将与数据一样多。

Here's a SqlFiddle of the resulting table projected by the above query. 这是上述查询投影的结果表的SqlFiddle

From here, it is a simple task to pull the resultset into an ADO data reader or bind this to your drop down list (eg set the .DataSource to the data reader, and call .DataBind() on the DropDownList ). 从这里开始,将结果集拉入ADO数据读取器或将其绑定到您的下拉列表是一个简单的任务(例如,将.DataSource设置为数据读取器,并在DropDownList上调用.DataBind() )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM