XmlDocument.CreateCDataSection(String) Método

Definición

Crea un XmlCDataSection que contiene los datos especificados.

public:
 virtual System::Xml::XmlCDataSection ^ CreateCDataSection(System::String ^ data);
public virtual System.Xml.XmlCDataSection CreateCDataSection (string data);
public virtual System.Xml.XmlCDataSection CreateCDataSection (string? data);
abstract member CreateCDataSection : string -> System.Xml.XmlCDataSection
override this.CreateCDataSection : string -> System.Xml.XmlCDataSection
Public Overridable Function CreateCDataSection (data As String) As XmlCDataSection

Parámetros

data
String

Contenido del nuevo objeto XmlCDataSection.

Devoluciones

XmlCDataSection

Nuevo objeto XmlCDataSection.

Ejemplos

En el ejemplo siguiente se crea un nodo CDATA y se agrega al documento.

#using <System.Xml.dll>

using namespace System;
using namespace System::IO;
using namespace System::Xml;
int main()
{
   XmlDocument^ doc = gcnew XmlDocument;
   doc->LoadXml( "<book genre='novel' ISBN='1-861001-57-5'><title>Pride And Prejudice</title></book>" );
   
   //Create a CData section.
   XmlCDataSection^ CData;
   CData = doc->CreateCDataSection( "All Jane Austen novels 25% off starting 3/23!" );
   
   //Add the new node to the document.
   XmlElement^ root = doc->DocumentElement;
   root->AppendChild( CData );
   Console::WriteLine( "Display the modified XML..." );
   doc->Save( Console::Out );
}

using System;
using System.IO;
using System.Xml;

public class Sample
{
  public static void Main()
  {
    XmlDocument doc = new XmlDocument();
    doc.LoadXml("<book genre='novel' ISBN='1-861001-57-5'>" +
                "<title>Pride And Prejudice</title>" +
                "</book>");

    //Create a CData section.
    XmlCDataSection CData;
    CData = doc.CreateCDataSection("All Jane Austen novels 25% off starting 3/23!");

    //Add the new node to the document.
    XmlElement root = doc.DocumentElement;
    root.AppendChild(CData);

    Console.WriteLine("Display the modified XML...");
    doc.Save(Console.Out);
  }
}
Option Explicit
Option Strict

Imports System.IO
Imports System.Xml

Public Class Sample
    
    Public Shared Sub Main()
        Dim doc As New XmlDocument()
        doc.LoadXml("<book genre='novel' ISBN='1-861001-57-5'>"  & _
                    "<title>Pride And Prejudice</title>"  & _
                    "</book>")
        
        'Create a CData section.
        Dim CData As XmlCDataSection
        CData = doc.CreateCDataSection("All Jane Austen novels 25% off starting 3/23!")
        
        'Add the new node to the document.
        Dim root As XmlElement = doc.DocumentElement
        root.AppendChild(CData)
        
        Console.WriteLine("Display the modified XML...")
        doc.Save(Console.Out)
    End Sub
End Class

Comentarios

Aunque este método crea el nuevo objeto en el contexto del documento, no agrega automáticamente el nuevo objeto al árbol de documentos. Para agregar el nuevo objeto, debe llamar explícitamente a uno de los métodos de inserción de nodo.

Según la recomendación W3C Extensible Markup Language (XML) 1.0, los nodos CDataSection se permiten dentro de los nodos Element y en los nodos EntityReference cuando el nodo EntityReference no es un elemento secundario de un nodo Attribute.

Se aplica a