コード例 #1
0
        public void RemoveNodes()
        {
            Aspose.Words.Document doc = new Aspose.Words.Document();

            //ExStart
            //ExFor:Node
            //ExFor:Node.NodeType
            //ExFor:Node.Remove
            //ExSummary:Shows how to remove all nodes of a specific type from a composite node. In this example we remove tables from a section body.
            // Get the section that we want to work on.
            Aspose.Words.Section section = doc.Sections[0];
            Body body = section.Body;

            // Select the first child node in the body.
            Aspose.Words.Node curNode = body.FirstChild;

            while (curNode != null)
            {
                // Save the pointer to the next sibling node because if the current
                // node is removed from the parent in the next step, we will have
                // no way of finding the next node to continue the loop.
                Aspose.Words.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;
            }
            //ExEnd
        }
コード例 #2
0
        /// <summary>
        /// Removes nodes from start up to but not including the end node.
        /// Start and end are assumed to have the same parent.
        /// </summary>
        private static void RemoveSameParent(Aspose.Words.Node startNode, Aspose.Words.Node endNode)
        {
            if ((endNode != null) && (startNode.ParentNode != endNode.ParentNode))
            {
                throw new ArgumentException("Start and end nodes are expected to have the same parent.");
            }

            Aspose.Words.Node curChild = startNode;
            while ((curChild != null) && (curChild != endNode))
            {
                Aspose.Words.Node nextChild = curChild.NextSibling;
                curChild.Remove();
                curChild = nextChild;
            }
        }