XmlNode.RemoveAll 方法

定義

移除目前節點的所有子節點和/或屬性。

public:
 virtual void RemoveAll();
public virtual void RemoveAll ();
abstract member RemoveAll : unit -> unit
override this.RemoveAll : unit -> unit
Public Overridable Sub RemoveAll ()

範例

下列範例會從根節點移除所有子節點和屬性節點。

#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 all attribute and child nodes.
   root->RemoveAll();
   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 all attribute and child nodes.
    root.RemoveAll();

    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 all attribute and child nodes.
        root.RemoveAll()
        
        Console.WriteLine("Display the modified XML...")
        doc.Save(Console.Out)
    End Sub
End Class

備註

如果已知已移除的屬性具有預設值,則會立即顯示包含預設值的屬性,如果適用的話,對應的命名空間 URI、本機名稱和前置詞。

此方法是檔物件模型 (DOM) 的 Microsoft 延伸模組。

給繼承者的注意事項

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

適用於