The reference node is not a child of this node

I have numerous files that I am trying to append to the final destination document. The following code is what is found in the wiki however it only seems to work the first time. When I try to append sections from the second file that needs to be copied over I get an "The reference node is not a child of this node" exception thrown from the last line (dstDoc.Sections[dstDoc.Sections.Count - 1].AppendContent(newSection);). Any ideas why?

Aspose.Words.Document srcDoc = new Aspose.Words.Document(filename);
Aspose.Words.Document dstDoc = docBuilder.Document;

Aspose.Words.Section newSection = (Aspose.Words.Section) dstDoc.ImportNode(section, true);
dstDoc.Sections[dstDoc.Sections.Count - 1].AppendContent(newSection);

Here is a tidbit from the stacktrace... StackTrace " at Aspose.Words.CompositeNode.؅(Node ۰, Node ۲, Boolean ി)\r\n at Aspose.Words.CompositeNode.InsertAfter(Node newChild, Node refChild)\r\n at Aspose.Words.CompositeNode.۳(Node Ԛ, Node ं, Node ര)\r\n at Aspose.Words.Section.ᓼ(Section ᓽ, Boolean ി)\r\n at Aspose.Words.Section.AppendContent(Section sourceSection)\r\n

The PrependContent doesn't throw the error... however it obviously inserts the content [in my case] backwards.

The following code works ok in my test:

private void AppendDocuments()

{

string filename = Application.StartupPath + @"\Doc1.doc";

Document doc = new Document(filename);

AppendDocument(doc, new Document(Application.StartupPath + @"\List1.doc"));

AppendDocument(doc, new Document(Application.StartupPath + @"\List2.doc"));

doc.Save(Application.StartupPath + @"\Doc1 Out.doc");

}

private void AppendDocumentContent(Document dst, Document src)

{

foreach(Section section in src.Sections)

{

Section appendedSection = (Section)dst.ImportNode(section, true);

dst.LastSection.AppendContent(appendedSection);

}

}

Please provide more information, so that I could reproduce the error.

Does the following work for you?

private void AppendDocuments()

{

string filename = Application.StartupPath + @"\Doc1.doc";

Document doc = new Document(filename);

AppendDocument(doc, new Document(Application.StartupPath + @"\List1.doc", 0));

AppendDocument(doc, new Document(Application.StartupPath + @"\List2.doc", 1));

AppendDocument(doc, new Document(Application.StartupPath + @"\List1.doc", 0));

doc.Save(Application.StartupPath + @"\Doc1 Out.doc");

}

private void AppendDocumentContent(Document dst, Document src, int sectionIndex)

{

foreach(Section section in src.Sections)

{

Section appendedSection = (Section)dst.ImportNode(section, true);

dst.Sections[sectionIndex].AppendContent(appendedSection);

}

}

Yes, it works. Please send me the documents you are testing with - maybe then I will be able to reproduce the error.

Try something like this instead:

foreach (Section section in src.Sections)
{
Section appendedSection = (Section)output.ImportNode(section, true);
dst.Sections.Insert(dst.Sections.Count + 1, appendedSection);
}