在 Xml 变量中存储查询结果时如何保留 CDATA 标记?

发布于 2025-01-02 05:30:02 字数 2044 浏览 0 评论 0原文

当我使用 For Explicit 在 Sql Server 2008 R2 中生成 Xml 时(因为我的消费者想要将其中一个元素包装在 CDATA 中)并将结果存储在 Xml 变量中,我想要包装在 CDATA 标记中的数据不再显示在 CDATA 标记中。如果我不将 For Xml Explicit 结果推送到 Xml 变量中,则保留 CDATA 标记。我使用 @Xml 变量作为 .Net 中的 SqlParameter。

在此示例中,第一个选择 (Select @Xml) 没有将 Line2 封装在 CDATA 标记中。但是第二个选择(用于填充 @Xml 变量的相同查询)确实具有包裹 Line2 列的 CDATA 标记。

Declare @Xml Xml

Begin Try
    Drop Table #MyTempTable
End Try
Begin Catch
End Catch

Select
    'Record' As Record
    , 'Line1' As Line1
    , 'Line2' As Line2
Into
    #MyTempTable

Select @Xml =
(
    Select
        x.Tag
        , x.Parent
        , x.[Root!1]
        , x.[Record!2!Line1!Element]
        , x.[Record!2!Line2!cdata]
    From
        (
            Select
                1 As Tag, Null As Parent
                , Null As [Root!1]
                , Null As [Record!2!Line1!Element]
                , Null As [Record!2!Line2!cdata]
            From
                #MyTempTable
            Union
            Select
                2 As Tag, 1 As Parent
                , Null As [Root!1]
                , Line1 As [Record!2!Line1!Element]
                , Line2 As [Record!2!Line2!cdata]
            From
                #MyTempTable
        ) x
    For
        Xml Explicit
)

Select @Xml

    Select
        x.Tag
        , x.Parent
        , x.[Root!1]
        , x.[Record!2!Line1!Element]
        , x.[Record!2!Line2!cdata]
    From
        (
            Select
                1 As Tag, Null As Parent
                , Null As [Root!1]
                , Null As [Record!2!Line1!Element]
                , Null As [Record!2!Line2!cdata]
            From
                #MyTempTable
            Union
            Select
                2 As Tag, 1 As Parent
                , Null As [Root!1]
                , Line1 As [Record!2!Line1!Element]
                , Line2 As [Record!2!Line2!cdata]
            From
                #MyTempTable
        ) x
    For
        Xml Explicit

Begin Try
    Drop Table #MyTempTable
End Try
Begin Catch
End Catch

When I generate Xml in Sql Server 2008 R2 using For Explicit (because my consumer wants one of the elements wrapped in CDATA) and store the results in an Xml variable, the data I want wrapped in CDATA tags no longer appears wrapped in CDATA tags. If I don't push the For Xml Explicit results into an Xml variable then the CDATA tags are retained. I am using the @Xml variable as an SqlParameter from .Net.

In this example, the first select (Select @Xml) does not have Line2 wrapped in CDATA tags. But the second select (the same query used to populate the @Xml variable) does have the CDATA tags wrapping the Line2 column.

Declare @Xml Xml

Begin Try
    Drop Table #MyTempTable
End Try
Begin Catch
End Catch

Select
    'Record' As Record
    , 'Line1' As Line1
    , 'Line2' As Line2
Into
    #MyTempTable

Select @Xml =
(
    Select
        x.Tag
        , x.Parent
        , x.[Root!1]
        , x.[Record!2!Line1!Element]
        , x.[Record!2!Line2!cdata]
    From
        (
            Select
                1 As Tag, Null As Parent
                , Null As [Root!1]
                , Null As [Record!2!Line1!Element]
                , Null As [Record!2!Line2!cdata]
            From
                #MyTempTable
            Union
            Select
                2 As Tag, 1 As Parent
                , Null As [Root!1]
                , Line1 As [Record!2!Line1!Element]
                , Line2 As [Record!2!Line2!cdata]
            From
                #MyTempTable
        ) x
    For
        Xml Explicit
)

Select @Xml

    Select
        x.Tag
        , x.Parent
        , x.[Root!1]
        , x.[Record!2!Line1!Element]
        , x.[Record!2!Line2!cdata]
    From
        (
            Select
                1 As Tag, Null As Parent
                , Null As [Root!1]
                , Null As [Record!2!Line1!Element]
                , Null As [Record!2!Line2!cdata]
            From
                #MyTempTable
            Union
            Select
                2 As Tag, 1 As Parent
                , Null As [Root!1]
                , Line1 As [Record!2!Line1!Element]
                , Line2 As [Record!2!Line2!cdata]
            From
                #MyTempTable
        ) x
    For
        Xml Explicit

Begin Try
    Drop Table #MyTempTable
End Try
Begin Catch
End Catch

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

栀子花开つ 2025-01-09 05:30:02

你不能。 XML 数据类型不保留 CDATA 部分。

请查看此处有关该主题的讨论。

http://social.msdn.microsoft.com/forums/en-US/sqlxml/thread/e22efff3-192e-468e-b173-ced52ada857f/

You can't. The XML data type does not preserve CDATA sections.

Have a look here for a discussion about the subject.

http://social.msdn.microsoft.com/forums/en-US/sqlxml/thread/e22efff3-192e-468e-b173-ced52ada857f/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文