Overview
The XmlCDataSection class represents a CDATA section in an XML document. CDATA sections are used to include text data that should not be parsed by the XML parser.
Namespace
Assembly
Namespace: System.Xml
Assembly: System.Xml.dll
Syntax
public class XmlCDataSection : XmlCharacterData
Inheritance
Object → XmlNode → XmlCharacterData → XmlCDataSection
Members
Properties
| Name | Type | Description |
|---|---|---|
| Data | string | Gets or sets the text contained within the CDATA section. |
| InnerText | string | Gets the concatenated values of the node and all its child nodes. |
| OuterXml | string | Gets the markup representing this node and all its child nodes. |
Methods
| Name | Return Type | Description |
|---|---|---|
| CloneNode(bool deep) | XmlNode | Creates a duplicate of this node. |
Examples
The following example creates an XML document with a CDATA section containing JavaScript code.
using System;
using System.Xml;
class Program
{
static void Main()
{
XmlDocument doc = new XmlDocument();
XmlElement root = doc.CreateElement("script");
doc.AppendChild(root);
string js = @"function greet(name) {
alert('Hello, ' + name);
}";
XmlCDataSection cdata = doc.CreateCDataSection(js);
root.AppendChild(cdata);
Console.WriteLine(doc.OuterXml);
}
}
Output:
<script><![CDATA[function greet(name) {
alert('Hello, ' + name);
}]]></script>
Remarks
CDATA sections are useful when the content includes characters such as < and & that would otherwise need to be escaped. The XML parser treats the content of a CDATA section as literal text.
When using XmlDocument, you can create a CDATA section via XmlDocument.CreateCDataSection(string data).