Updatating Content Control throws exception "Cannot insert a node of this type at this location."

Hi
I am quite new to Aspose.words, I was trying a simple content control update for Plain Text field but I am getting “Cannot insert a node of this type at this location.” exception with my sample word file.
The code snippet and the my sample word file attached. Please let me know what is the issue with the code and how to fix it.
Karthik

Hi Karthik,

Thanks for your inquiry. Please use following code exapmle to achieve your requirements. Hope this helps you.

Document doc = new Document(MyDir + "mysection.docx");
NodeCollection sdtNodes = doc.getChildNodes(NodeType.STRUCTURED_DOCUMENT_TAG, true);
for (StructuredDocumentTag sdt : (Iterable)sdtNodes)
{
    if (sdt.getSdtType() == SdtType.PLAIN_TEXT)
    {
        sdt.removeAllChildren();
        Run run = new Run(doc);
        run.setText("New value of PLAIN_TEXT");
        sdt.appendChild(run);
    }
    else if (sdt.getSdtType() == SdtType.RICH_TEXT)
    {
        sdt.removeAllChildren();
        Paragraph para = new Paragraph(doc);
        Run run = new Run(doc);
        run.setText("New value of RICH_TEXT");
        para.appendChild(run);
        sdt.appendChild(para);
    }
}
doc.save(MyDir + "Out.docx");

Thanks, This helped me to resolve my issue.

Hi Karthik,

Thanks for your feedback. Please feel free to ask if you have any question about Aspose.Words, we will be happy to help you.