XmlNode.ReplaceChild(XmlNode, XmlNode) Metode

Definisi

Mengganti simpul oldChild anak dengan newChild simpul.

public:
 virtual System::Xml::XmlNode ^ ReplaceChild(System::Xml::XmlNode ^ newChild, System::Xml::XmlNode ^ oldChild);
public virtual System.Xml.XmlNode ReplaceChild(System.Xml.XmlNode newChild, System.Xml.XmlNode oldChild);
abstract member ReplaceChild : System.Xml.XmlNode * System.Xml.XmlNode -> System.Xml.XmlNode
override this.ReplaceChild : System.Xml.XmlNode * System.Xml.XmlNode -> System.Xml.XmlNode
Public Overridable Function ReplaceChild (newChild As XmlNode, oldChild As XmlNode) As XmlNode

Parameter

newChild
XmlNode

Simpul baru untuk dimasukkan ke dalam daftar anak.

oldChild
XmlNode

Simpul yang digantikan dalam daftar.

Mengembalikan

Simpul diganti.

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.

oldChild Bukan anak dari simpul ini.

Contoh

Contoh berikut menggantikan elemen judul dalam 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 title element.
    XmlElement elem = doc.CreateElement("title");
    elem.InnerText="The Handmaid's Tale";

    //Replace the title element.
    root.ReplaceChild(elem, root.FirstChild);

    Console.WriteLine("Display the modified XML...");
    doc.Save(Console.Out);
  }
}
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 title element.
        Dim elem As XmlElement = doc.CreateElement("title")
        elem.InnerText = "The Handmaid's Tale"
        
        'Replace the title element.
        root.ReplaceChild(elem, root.FirstChild)
        
        Console.WriteLine("Display the modified XML...")
        doc.Save(Console.Out)
    End Sub
End Class

Keterangan

newChild Jika sudah ada di pohon, pertama-tama dihapus.

newChild Jika dibuat dari dokumen lain, Anda bisa menggunakan XmlDocument.ImportNode untuk mengimpor simpul ke dokumen saat ini. Simpul yang diimpor kemudian dapat diteruskan ke ReplaceChild metode .

Catatan Bagi Inheritor

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

Berlaku untuk

Lihat juga