XmlDocument.CreateTextNode(String) Método

Definição

Cria um XmlText com o texto especificado.

public:
 virtual System::Xml::XmlText ^ CreateTextNode(System::String ^ text);
public virtual System.Xml.XmlText CreateTextNode(string text);
abstract member CreateTextNode : string -> System.Xml.XmlText
override this.CreateTextNode : string -> System.Xml.XmlText
Public Overridable Function CreateTextNode (text As String) As XmlText

Parâmetros

text
String

O texto do nó Texto.

Retornos

O novo nó.

Exemplos

O exemplo a seguir cria um novo elemento e o adiciona ao documento.

using System;
using System.Xml;

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

    // Create a new node and add it to the document.
    // The text node is the content of the price element.
    XmlElement elem = doc.CreateElement("price");
    XmlText text = doc.CreateTextNode("19.95");
    doc.DocumentElement.AppendChild(elem);
    doc.DocumentElement.LastChild.AppendChild(text);

    Console.WriteLine("Display the modified XML...");
    doc.Save(Console.Out);
  }
}
Imports System.IO
Imports System.Xml

Public Class Sample
    Public Shared Sub Main()

        ' Create the XmlDocument.
        Dim doc As New XmlDocument()
        doc.LoadXml("<book genre='novel' ISBN='1-861001-57-5'>"  & _
                    "<title>Pride And Prejudice</title>"  & _
                    "</book>")

        ' Create a new node and add it to the document.
        ' The text node is the content of the price element.
        Dim elem As XmlElement = doc.CreateElement("price")
        Dim text As XmlText = doc.CreateTextNode("19.95")
        doc.DocumentElement.AppendChild(elem)
        doc.DocumentElement.LastChild.AppendChild(text)

        Console.WriteLine("Display the modified XML...")
        doc.Save(Console.Out)

    End Sub
End Class

Comentários

Embora esse método crie o novo objeto no contexto do documento, ele não adiciona automaticamente o novo objeto à árvore de documentos. Para adicionar o novo objeto, você deve chamar explicitamente um dos métodos de inserção de nó.

De acordo com a recomendação XML (Linguagem de Marcação Extensível) 1.0 do W3C, nós de texto só são permitidos em nós de Elemento, Atributo e EntityReference.

Aplica-se a