XmlDocument.CreateCDataSection(String) Méthode

Définition

Crée un élément XmlCDataSection contenant les données spécifiées.

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

Paramètres

data
String

Contenu du nouvel élément XmlCDataSection.

Retours

XmlCDataSection

Nouvelle XmlCDataSection.

Exemples

L’exemple suivant crée un nœud CDATA et l’ajoute au document.

#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

Remarques

Bien que cette méthode crée le nouvel objet dans le contexte du document, il n’ajoute pas automatiquement le nouvel objet à l’arborescence de documents. Pour ajouter le nouvel objet, vous devez appeler explicitement l’une des méthodes d’insertion de nœud.

Selon la recommandation W3C Extensible Markup Language (XML) 1.0, les nœuds CDataSection sont autorisés dans les nœuds Élément et dans les nœuds EntityReference lorsque le nœud EntityReference n’est pas un enfant d’un nœud d’attribut.

S’applique à