XmlCDataSection Class

Introduction

Represents a <![CDATA[...]]> section in the XML document.

CDATA sections are used to escape blocks of text that contain characters that would otherwise be recognized as markup. The content inside a CDATA section is treated as raw character data, not as markup. This is particularly useful when dealing with XML documents that contain large amounts of text with characters like <, >, and &, which have special meaning in XML.

Inheritance Hierarchy

System.Object
    System.Xml.XmlNode
        System.Xml.XmlCharacterData
            System.Xml.XmlCDataSection

Syntax

public sealed class XmlCDataSection : System.Xml.XmlCharacterData

Constructors

The XmlCDataSection class does not expose any public constructors. Instances of this class are created by the XmlDocument class when you call methods such as CreateCDataSection.

Properties

Name Description
Name Gets the qualified name of the node. For XmlCDataSection, this is specifically #cdata-section.
NodeType Gets the type of the current node. Returns XmlNodeType.CDATA.
Value Gets or sets the text content of the CDATA section.
Data Gets or sets the text of this node. This property inherits from XmlCharacterData.

Methods

Name Description
CloneNode Creates a duplicate of this node.
WriteTo Saves all the children of the node to the specified XmlWriter.
InnerText Gets or sets the concatenated values of all the child nodes of this node.

Remarks

The XmlCDataSection class is part of the System.Xml namespace, which provides classes for working with XML documents.

When parsing XML, the XmlDocument class automatically handles CDATA sections. If you need to programmatically create a CDATA section, use the XmlDocument.CreateCDataSection method.

The content within a CDATA section should not contain the string ]]>, as this marks the end of the CDATA section.

Example

The following example demonstrates how to create an XmlCDataSection and add it to an XML document.

C# Example

using System;
using System.Xml;

public class CDataExample
{
    public static void Main(string[] args)
    {
        // Create a new XML document
        XmlDocument doc = new XmlDocument();

        // Create an element
        XmlElement element = doc.CreateElement("description");

        // Create a CDATA section
        XmlCDataSection cdata = doc.CreateCDataSection("This is some text containing < and > symbols. & needs escaping!");

        // Append the CDATA section to the element
        element.AppendChild(cdata);

        // Append the element to the document
        doc.AppendChild(element);

        // Save the XML document
        using System.IO.StringWriter sw = new System.IO.StringWriter();
        XmlWriter writer = XmlWriter.Create(sw, new XmlWriterSettings{ Indent= true });
        doc.Save(writer);

        // Output the XML
        Console.WriteLine(sw.ToString());
    }
}

Output:

<?xml version="1.0"?>
<description>
    <![CDATA[This is some text containing < and > symbols. & needs escaping!]]>
</description>

Requirements

Namespace: System.Xml

Assembly: System.Xml.dll

.NET Framework Versions: Available in the following versions: 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.6, 4.7, 4.8.