Search This Blog

Tuesday, November 26, 2013

Create CDATA section XML C#

C# > XML > XmlDocument > CreateCDataSection

CreateCDataSection creates an XmlCDataSection.
CDATA is a section used to quote or escape blocks of text to keep that text from being interpreted as markup language.

Example:

XmlDocument doc = new XmlDocument();
doc.LoadXml("<product ID='1'></ product>");
//Create a CData section
XmlCDataSection CData;
CData = doc.CreateCDataSection("Size is 10/55");

//Add the new node to the document
XmlElement root = doc.DocumentElement;
root.AppendChild(CData);
doc.Save("C:\\Products.xml");

Result:
  <![CDATA[Size is 10/55]]>
</product>