XmlDocument.CreateTextNode(String) XmlDocument.CreateTextNode(String) XmlDocument.CreateTextNode(String) XmlDocument.CreateTextNode(String) Method

Definition

Erstellt einen XmlText mit dem angegebenen Text.Creates an XmlText with the specified text.

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

Parameter

text
String String String String

Der Text für den Text-Knoten.The text for the Text node.

Gibt zurück

Der neue XmlText-Knoten.The new XmlText node.

Beispiele

Im folgenden Beispiel wird ein neues-Element erstellt und dem Dokument hinzugefügt.The following example creates a new element and adds it to the document.

#using <System.Xml.dll>

using namespace System;
using namespace System::IO;
using namespace System::Xml;
int main()
{
   
   //Create the XmlDocument.
   XmlDocument^ doc = gcnew 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.
   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 );
}

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

public class Sample
{
  public static void Main()
  {
    //Create the XmlDocument.
    XmlDocument doc = 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.
    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);

  }
}
Option Explicit
Option Strict

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

Hinweise

Obwohl diese Methode das neue-Objekt im Kontext des Dokuments erstellt, wird das neue-Objekt nicht automatisch der Dokumentstruktur hinzugefügt.Although this method creates the new object in the context of the document, it does not automatically add the new object to the document tree. Um das neue-Objekt hinzuzufügen, müssen Sie explizit eine der Knoten Einfügemethoden aufzurufen.To add the new object, you must explicitly call one of the node insert methods.

Gemäß der Empfehlung W3C Extensible Markup Language (XML) 1,0sind Text Knoten nur innerhalb der Knoten Element, Attribute und EntityReference zulässig.According to the W3C Extensible Markup Language (XML) 1.0 recommendation, Text nodes are only allowed within Element, Attribute and EntityReference nodes.

Gilt für: