XmlNode.RemoveChild(XmlNode) 方法

定義

移除指定的子節點。

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

參數

oldChild
XmlNode

正在移除的節點。

傳回

XmlNode

移除的節點。

例外狀況

oldChild 不是這個節點的子節點。 或者這個節點是唯讀的。

範例

下列範例會從 XML 檔中移除節點。

#using <System.Xml.dll>

using namespace System;
using namespace System::IO;
using namespace System::Xml;
int main()
{
   XmlDocument^ doc = gcnew XmlDocument;
   doc->LoadXml( "<book genre='novel' ISBN='1-861001-57-5'>"
   "<title>Pride And Prejudice</title>"
   "</book>" );
   XmlNode^ root = doc->DocumentElement;
   
   //Remove the title element.
   root->RemoveChild( root->FirstChild );
   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() {

    XmlDocument doc = new XmlDocument();
    doc.LoadXml("<book genre='novel' ISBN='1-861001-57-5'>" +
                "<title>Pride And Prejudice</title>" +
                "</book>");

    XmlNode root = doc.DocumentElement;

    //Remove the title element.
    root.RemoveChild(root.FirstChild);

    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
        
        'Remove the title element.
        root.RemoveChild(root.FirstChild)
        
        Console.WriteLine("Display the modified XML...")
        doc.Save(Console.Out)
    End Sub
End Class

給繼承者的注意事項

在衍生類別中覆寫 RemoveChild 時,若要正確引發事件,您必須呼叫 RemoveChild 基類的 方法。

適用於