How to write CData in xml

Do you really need it to be in CDATA, or do you just want to get the text in there in a way which won't require extra escaping in your code?

InnerText performs whatever escaping is required, so generally I'd just use

xnode.InnerText = Convert.ToString(sqlReader["story_status"]);

... but if you really want a CDATA node, you can create one yourself as per Nekresh's answer.


As described here: msdn

// Create an XmlCDataSection from your document
var cdata = xdoc.CreateCDataSection(Convert.ToString(sqlReader["story_status"]));

// Append the cdata section to your node
xnode.AppendChild(cdata);

Tags:

C#

Xml