当我使用For Explicit在Sql Server 2008 R2中生成Xml时(因为我的消费者需要CDATA中包含的其中一个元素)并将结果存储在Xml变量中,我想要包含在CDATA标记中的数据不再显示为包含在CDATA标签。如果我不将For Xml Explicit结果推送到Xml变量中,则保留CDATA标记。我使用@Xml变量作为.Net的SqlParameter。
在此示例中,第一个选择(选择@Xml)不将Line2包装在CDATA标记中。但是第二个选择(用于填充@Xml变量的相同查询)确实包含了Line2列的CDATA标记。</ p>
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
答案 0 :(得分:3)
你做不到。 XML数据类型不保留CDATA部分。
在这里查看有关该主题的讨论。
http://social.msdn.microsoft.com/forums/en-US/sqlxml/thread/e22efff3-192e-468e-b173-ced52ada857f/