XmlNode.AppendChild(XmlNode) Metode

Definisi

Menambahkan simpul yang ditentukan ke akhir daftar simpul anak, dari simpul ini.

public:
 virtual System::Xml::XmlNode ^ AppendChild(System::Xml::XmlNode ^ newChild);
public virtual System.Xml.XmlNode AppendChild(System.Xml.XmlNode newChild);
public virtual System.Xml.XmlNode? AppendChild(System.Xml.XmlNode newChild);
abstract member AppendChild : System.Xml.XmlNode -> System.Xml.XmlNode
override this.AppendChild : System.Xml.XmlNode -> System.Xml.XmlNode
Public Overridable Function AppendChild (newChild As XmlNode) As XmlNode

Parameter

newChild
XmlNode

Simpul yang akan ditambahkan. Semua konten simpul yang akan ditambahkan dipindahkan ke lokasi yang ditentukan.

Mengembalikan

Node ditambahkan.

Pengecualian

Simpul ini berjenis yang tidak mengizinkan simpul anak dari jenis newChild simpul.

newChild adalah leluhur dari simpul ini.

newChild Dibuat dari dokumen yang berbeda dari yang membuat simpul ini.

Simpul ini bersifat baca-saja.

Contoh

Contoh berikut menambahkan simpul baru ke dokumen XML.

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>");

    XmlNode root = doc.DocumentElement;

    //Create a new node.
    XmlElement elem = doc.CreateElement("price");
    elem.InnerText="19.95";

    //Add the node to the document.
    root.AppendChild(elem);

    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>")
        
        Dim root As XmlNode = doc.DocumentElement
        
        'Create a new node.
        Dim elem As XmlElement = doc.CreateElement("price")
        elem.InnerText = "19.95"
        
        'Add the node to the document.
        root.AppendChild(elem)
        
        Console.WriteLine("Display the modified XML...")
        doc.Save(Console.Out)
    End Sub
End Class

Output:

Display the modified XML...
<?xml version="1.0" encoding="IBM437"?>
<book genre="novel" ISBN="1-861001-57-5">
  <title>Pride And Prejudice</title>
  <price>19.95</price>

Keterangan

newChild Jika sudah ada di pohon, itu dihapus dari posisi aslinya dan ditambahkan ke posisi targetnya. Untuk informasi selengkapnya tentang menyisipkan simpul, lihat Menyisipkan Simpul ke dalam Dokumen XML.

Jika simpul yang disisipkan dibuat dari dokumen lain, Anda bisa menggunakan XmlDocument.ImportNode untuk mengimpor simpul ke dokumen saat ini. Simpul yang diimpor kemudian dapat dimasukkan ke dalam dokumen saat ini.

Catatan Bagi Inheritor

Saat mengambil alih di kelas turunan AppendChild , agar peristiwa dinaikkan dengan benar, Anda harus memanggil AppendChild metode kelas dasar.

Berlaku untuk

Lihat juga