Question

We are using SQL Server 2005 and are trying to store an XML Type in the database. The XML type has an element that needs to contain the content in CDATA, yet once inserted, the field seems to be stripping the CDATA and storing the element without it...

Has anybody experienced or even resolved this in the past?

Handy example:

create table t (x xml)

insert into t values  ('<test>kjhghk</test>')
insert into t  values ('<test><![CDATA[kjhghk]]></test>')
select * from t

drop table t

results:

<test>kjhghk</test>
<test>kjhghk</test>
Was it helpful?

Solution

Unfortunately, this is standard behavior where the CDATA section is stripped and its content entitized. You could use the cdata directive of FOR XML EXPLICIT to add the content to a CDATA section when retrieving, but depending on the complexity of your XML this may be combersome. Also, see this post.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top