XContainer.RemoveNodes 方法

定义

从此文档或元素中移除子节点。Removes the child nodes from this document or element.

public:
 void RemoveNodes();
public void RemoveNodes ();
member this.RemoveNodes : unit -> unit
Public Sub RemoveNodes ()

示例

下面的示例创建一个包含一些子节点的 XML 树。The following example creates an XML tree that contains some child nodes. 然后,它调用此方法来移除子节点。It then calls this method to remove the child nodes.

XElement xmlTree = new XElement("Root",  
    new XElement("Child1", 1),  
    new XElement("Child2", 2),  
    new XComment("a comment"),  
    new XElement("Child3", 3),  
    new XElement("Child4", 4),  
    new XText("mixed content"),  
    new XElement("Child5", 5)  
);  
xmlTree.RemoveNodes();  
Console.WriteLine(xmlTree);  
Dim xmltree As XElement = _   
    <Root>  
        <Child1>1</Child1>  
        <Child2>2</Child2>  
        <!--a comment-->  
        <Child3>3</Child3>  
        <Child4>4</Child4>mixed content<Child5>5</Child5>  
    </Root>  
  
xmltree.RemoveNodes()  
Console.WriteLine(xmltree)  

该示例产生下面的输出:This example produces the following output:

<Root />  

注解

如果对包含属性的元素调用此方法,则此方法不会删除属性。If you call this method on an element that contains attributes, this method will not remove the attributes. 若要删除元素的属性,请使用 RemoveAttributesTo remove the attributes of an element, use RemoveAttributes.

Remove 方法具有相关的功能。The Remove method has related functionality. 它删除集合中每个节点的子节点。It removes the child nodes of every node in a collection.

此方法将引发 ChangedChanging 事件。This method will raise the Changed and the Changing events.

适用于

另请参阅