sql-server – 带CDATA的SQL Server XML输出

前端之家收集整理的这篇文章主要介绍了sql-server – 带CDATA的SQL Server XML输出前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
有没有办法让sql Server XML返回使用CDATA?我有sql Server返回的XML,如下所示:
<locations>
    <site id="124">
        <sitename>Texas A &amp; M</sitename>
    </site>
</locations>

当我需要这个:

<locations>
    <site id="124">
        <sitename><![CDATA[Texas A & M]]></sitename>
    </site>
</locations>

解决方法

查看 FOR XML EXPLICIT(参数指令)的选项.它提供了更大程度的控制,您还可以指定CDATA.这是 a good tutorial.

并从该教程中添加代码

declare @agent table
(    
    AgentID int,Fname varchar(5),SSN varchar(11)
)

insert into @agent
select 1,'Vimal','123-23-4521' union all
select 2,'Jacob','321-52-4562' union all
select 3,'Tom','252-52-4563'

SELECT
    1 AS Tag,NULL AS Parent,NULL AS 'Agents!1!',NULL AS 'Agent!2!AgentID',NULL AS 'Agent!2!Fname!Element',NULL AS 'Agent!2!SSN!cdata'
UNION ALL
SELECT
    2 AS Tag,1 AS Parent,NULL,AgentID,Fname,SSN
FROM @agent
FOR XML EXPLICIT

猜你在找的MsSQL相关文章