NodeRemove Method

Removes itself from the parent.

Namespace:  Aspose.Words
Assembly:  Aspose.Words (in Aspose.Words.dll) Version: 20.8.0

Syntax

public void Remove()

Examples

Shows how to remove all nodes of a specific type from a composite node.
Document doc = new Document(MyDir + "Tables.docx");

Assert.AreEqual(2, doc.GetChildNodes(NodeType.Table, true).Count);

// Select the first child node in the body
Node curNode = doc.FirstSection.Body.FirstChild;

while (curNode != null)
{
    // Save the next sibling node as a variable in case we want to move to it after deleting this node
    Node nextNode = curNode.NextSibling;

    // A section body can contain Paragraph and Table nodes
    // If the node is a Table, remove it from the parent
    if (curNode.NodeType.Equals(NodeType.Table))
        curNode.Remove();

    // Continue going through child nodes until null (no more siblings) is reached
    curNode = nextNode;
}

Assert.AreEqual(0, doc.GetChildNodes(NodeType.Table, true).Count);
Shows how to delete all images from a document.
Document doc = new Document(MyDir + "Images.docx");
Assert.AreEqual(10, doc.GetChildNodes(NodeType.Shape, true).Count);

// Here we get all shapes from the document node, but you can do this for any smaller
// node too, for example delete shapes from a single section or a paragraph
NodeCollection shapes = doc.GetChildNodes(NodeType.Shape, true);

// We cannot delete shape nodes while we enumerate through the collection
// One solution is to add nodes that we want to delete to a temporary array and delete afterwards
ArrayList shapesToDelete = new ArrayList();

// Several shape types can have an image including image shapes and OLE objects
foreach (Shape shape in shapes.OfType<Shape>())
    if (shape.HasImage)
        shapesToDelete.Add(shape);

// Now we can delete shapes
foreach (Shape shape in shapesToDelete)
    shape.Remove();

// The only remaining shape doesn't have an image
Assert.AreEqual(1, doc.GetChildNodes(NodeType.Shape, true).Count);
Assert.False(((Shape)doc.GetChild(NodeType.Shape, 0, true)).HasImage);

See Also