Cannot insert a node of this type at this location exception with StructuredDocumentTag

Hi,
(using v11.3.0.0)

I’m trying to update the content of a “Content Control” (StructuredDocumentTag) that I use as placeholder with some multi-line content.

It worked with a very basic sample word document but did not work with my real word document. I receive a ‘System.ArgumentException: Cannot insert a node of this type at this location.’ exception…

I manage to recreate two identical documents, one is working, the other is not (see attached). Do you know what is the problem and how to fix it?

I may be going at this the wrong way, is there a better way to do update a specific region with text… it’s not a mail merge, so I don’t know if Fields can help.

Thanks,

Dominic.

Here’s the code I used.

private static void UpdateAspose()
{
    Aspose.Words.Document doc = new Aspose.Words.Document("ToUpdate.docx");

    Aspose.Words.NodeCollection sdts = doc.GetChildNodes(Aspose.Words.NodeType.StructuredDocumentTag, true);

    foreach (Aspose.Words.Markup.StructuredDocumentTag sdt in sdts)
    {
        if (sdt.Title == "WeightAndDimensions")
        {
            // Reset IsShowingPlaceholderText to hide placeholder text.
            sdt.IsShowingPlaceholderText = false;

            // Remove childnodes of SDT.
            sdt.ChildNodes.Clear();

            StringBuilder sb = new StringBuilder();
            sb.AppendLine(@"Line 1");
            sb.AppendLine(@"Line 2");
            sb.AppendLine(@"Line 3");
            sb.Append(@"Line 4");
            sdt.AppendChild(new Aspose.Words.Run(doc, sb.ToString()));
        }
    }

    doc.Save("UpdateAfter.docx");
}

Hi Dominic,

Thanks for your inquiry.

While using the latest version of Aspose.Words i.e. 11.3.0, I managed to reproduce this exception on my side. I have logged this issue in our bug tracking system. The issue ID is WORDSNET-6397. Your request has also been linked to this issue and you will be notified as soon as it is resolved.

Sorry for the inconvenience.

Best Regards,

Hi,

Thanks for the reply. Can you think of a workaround in the mean time?

Thanks,

Dominic.

Hi,

Thanks for your request. In this case, I am afraid, I cannot suggest you any programmatic workaround. So, you will have to wait for the fix. We will keep you informed and immediately let you know via this forum thread once this problem is resolved. We apologize for your inconvenience.

Best regards,

Hello,

I am having the exact same issue. I need to replace existing simple SDT “placeholder” content with either multiline text or a table depending upon program logic. I see code examples for building new SDT content with tables, etc. but not best practice code for navigating to an existing SDT in a document and replacing it with multiline or table content. Or am I missing something? I am still pretty new to the Aspose.Words library.

Thanks.

Hi
Ramona,

Thanks for your inquiry. It would be great if you could attach a sample Word document containing the SDT you wanted to replace with multi line text or Table. Also, please attach your target Word document here for testing. We will investigate as to how you are expecting your output document to be generated like and provide you code snippet.

Best Regards,

Hi Dominic,

Thanks for your patience. Please make the following corrections in your code and let us know if this solves your problem?

Document doc = new Document(@"C:\Temp\ToUpdate-NOTWORKING.docx");
Aspose.Words.NodeCollection sdts = doc.GetChildNodes(Aspose.Words.NodeType.StructuredDocumentTag, true);
foreach (Aspose.Words.Markup.StructuredDocumentTag sdt in sdts)
{
    if (sdt.Title == "WeightAndDimensions")
    {
        // Reset IsShowingPlaceholderText to hide placeholder text.
        sdt.IsShowingPlaceholderText = false;
        // Remove childnodes of SDT.
        sdt.ChildNodes.Clear();
        Paragraph para = new Paragraph(doc);
        StringBuilder sb = new StringBuilder();
        sb.AppendLine(@"Line 1");
        sb.AppendLine(@"Line 2");
        sb.AppendLine(@"Line 3");
        sb.Append(@"Line 4");
        para.AppendChild(new Run(doc, sb.ToString()));
        sdt.AppendChild(para);
    }
}
doc.Save(@"C:\Temp\out.docx");

Best Regards,

The issues you have found earlier (filed as WORDSNET-6397) have been fixed in this .NET update and this Java update.

This message was posted using Notification2Forum from Downloads module by aspose.notifier.