How to get a specific rich text content control by Tag

Hi

This question has been posted in an old thread, but I am making a new one seeing as the below one is 3 years old.
How to extract content of a content control by using the value of its tag

Basically I would like to be able to find a specific rich text content control by Tag in a docx file, without having to loop through all the controls in the document. Then I want to be able to read all the content in the content control.

Is this possible with Aspose and what version?

Kind Regards
Rob

Hi Rob,

Thanks for your inquiry.

Please note that Aspose.Words is quite different from the Microsoft Word’s Object Model in that it represents the document as a tree of objects more like an XML DOM tree. If you worked with any XML DOM library you will find it is easy to understand and work with Aspose.Words. When you load a Word document into Aspose.Words, it builds its DOM and all document elements and formatting are simply loaded into memory. Please read the following articles for more information on DOM:
https://docs.aspose.com/words/net/aspose-words-document-object-model/
https://docs.aspose.com/words/net/logical-levels-of-nodes-in-a-document/

The StructuredDocumentTag class represents a structured document tag (SDT or content control) in a document.

The StructuredDocumentTag.Tag property specifies a tag associated with the current SDT node. A tag is an arbitrary string which applications can associate with SDT in order to identify it without providing a visible friendly name.

The CompositeNode.GetChildNodes method returns a live collection of child nodes that match the specified type. Please use this method to get the specific type of node collection as shown below:

var doc = new Document(MyDir + "in.docx");
foreach(StructuredDocumentTag sdt in doc.GetChildNodes(NodeType.StructuredDocumentTag, true))
{
    if (sdt.Tag == "tagname")
    {
        // your code
        // You can get the child nodes of StructuredDocumentTag by using StructuredDocumentTag.GetChildNodes method
        Console.Write(sdt.ToString(SaveFormat.Text));
    }
}