Cannot change value for content control of type rich text box with Level = MarkupLevel.Inline

Error “cannot insert a node of this type at this location” is shown when changing value for content control of type rich text box with Level = MarkupLevel.Inline. Please see more details in the attachment.

if (sdt.Level == MarkupLevel.Inline && string.Equals(sdt.Title, "ANNEX_INSTRUMENT", StringComparison.InvariantCultureIgnoreCase))
{
    sdt.RemoveAllChildren();
    Paragraph para1 = new Paragraph(doc);
    Run run1 = new Run(doc);
    run1.Text = txtText.Text;
    para1.Runs.Add(run1);
    sdt.ChildNodes.Add(para1); => error with this line.
}

Hi there,

Thanks for your inquiry.

Please use following highlighted code snippet to fix the shared issue and let us know if you have any more queries.

Document doc = new Document(wordFilePath);
if (doc != null)
{
    foreach (StructuredDocumentTag sdt in doc.GetChildNodes(NodeType.StructuredDocumentTag, true))
    {
        if (sdt.SdtType == SdtType.RichText)
        {
            if (sdt.Level == MarkupLevel.Block && string.IsNullOrWhiteSpace(sdt.Title))
            {
                sdt.RemoveAllChildren();
                Paragraph para1 = new Paragraph(doc);
                Run run1 = new Run(doc);
                run1.Text = txtText.Text;
                para1.Runs.Add(run1);
                sdt.ChildNodes.Add(para1);
            }
            if (sdt.Level == MarkupLevel.Inline && string.Equals(sdt.Title, "ANNEX_INSTRUMENT", StringComparison.InvariantCultureIgnoreCase))
            {
                sdt.RemoveAllChildren();
                Run run1 = new Run(doc);
                run1.Text = txtText.Text;
                sdt.ChildNodes.Add(run1);
            }
        }
    }
}
doc.Save(outputPath);

Thank you for your answer, Tahir.
It helps me to solve the issue.

Hi there,

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