共用方式為


XmlDocument.CreateTextNode(String) 方法

定義

建立具有指定文字的 XmlText

public:
 virtual System::Xml::XmlText ^ CreateTextNode(System::String ^ text);
public virtual System.Xml.XmlText CreateTextNode (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

參數

text
String

Text 節點的文字。

傳回

XmlText

新的 XmlText 節點。

範例

下列範例會建立新的 元素,並將其新增至檔。

#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

備註

雖然這個方法會在檔的內容中建立新的 物件,但不會自動將新物件加入檔樹狀結構。 若要新增物件,您必須明確呼叫其中一個節點插入方法。

根據 W3C 可延伸標記語言 (XML) 1.0 建議,文位元組點只能在 Element、Attribute 和 EntityReference 節點內使用。

適用於