ImportNode(...)

I found this forum post: https://forum.aspose.com/t/123252
I’m trying to assign an Aspose.Words.Section variable to the variable “section” in this loop once my conditional logic has been satisifed (only interested in a section that has a whole set of bookmarks):

foreach(Section section in doc.Sections)

The reason I’m doing this is because I’m trying to avoid having to call the method:

insertedSection = (Section)dstDoc.ImportNode(section, true, ImportFormatMode.KeepSourceFormatting);

as little as possible because of slow performance. When I try to reinsert the same section from the source document into the destination document (just different bookmark results) with this code,

master.Sections.Insert(index++, lastSession);

I get the error, “The newChild was created from a different document than the one that created this node.”
Is there a workaround for this?
Thanks!

This message was posted using Aspose.Live 2 Forum

Hi
Thanks for your request. This occurs because lastSession is not child of master document. You should use ImportNode method in this case. See the following code for example.

master.Sections.Insert(index++, master.ImportNode(lastSession , true, ImportFormatMode.KeepSourceFormatting));

Hope this helps.
Best regards.

Thanks for the quick reply.

I already did ImportNode earlier, and saved the section like this:

insertedSection = (Section)master.ImportNode(section, true, ImportFormatMode.KeepSourceFormatting);
lastSession = section;

Later down in my outer loop I want to use the imported node again (just the bookmark contents are different within my dataset, I just need the bare-bones imported node from the source doc)

So instead of using ImportNode again, I’d like to just do this:

master.Sections.Insert(index++, lastSession);

I know it sounds like a repeat of my earlier post, but I’m trying not to use ImportNode(…) method twice if I already gotten the contents already (performance issue). Is this possible?

Hi

lastSection is section in the source document .

lastSession = section;

I think that you can clone imported Section. See the following snippet.

lastSession = (Section)insertedSection.Clone(true);

Hope this helps.
Best regards.

That’s exactly what I’m looking for. Hopefully it doesn’t give me the same error. The earlier post was trying with sections, I’m going to try with nodes from the same post:

Error when copy the content of one document to another

I’ll also give the section clone a shot too.

I tried to clone the section. It works, but only for two iterations through my outer loop (which assigns new bookmark values to a group of repeated fields). The cloned section seems to quit working at this point. I’m going to try:

master.Sections.RemoveAt(index);
master.Range.Bookmarks["FieldName")].Remove(); 

at each iteration through the outer loop. Do you have any other suggestions?

Thanks!

Hi
Thanks for your request. Unfortunately it is not quite clear for me what you are trying to achieve. Could you please explain me? Also please provide me your code and attach your documents.
Best regards.

Hi Alexey,

Thanks for being patient. I cut down my application to just a test one. The real master.doc document is a lot larger.

Please put the Master.doc and Source.doc into your C:\

  1. Test.zip has the attempt I did using sections and cloning the last section as you instructed. I want to try to use ImportNode only once and then reapply it when I have a new set of bookmarks from the source document. ImportNode seems to take a long time to execute so I’d like to avoid having to do it at every iteration of the loop.

  2. Test2.zip has the attempt from the section, “Insert a Document at Any Location” from https://docs.aspose.com/words/net/insert-and-append-documents/
    Again with the same concept of using ImportNode for the whole source document once. Then reuse it again via ArrayList for a new copied set of bookmarks.

Note: If you run a test on C:\master.doc. You have to replace it with the original master.doc again from Test2 or Test1 folder to reproduce the issue I have. Both WindowsApplication1 and WindowsApplication2 use the same master.doc and source.doc.
I’m aware of the NodeImporter class as described in:
https://forum.aspose.com/t/124131
Unfortunately, I was instructed to keep using our current version of 3.5.1 and try to improve on our current process because it’s the .dll we use in production. What Roman said sounds impressive and would probably fix our performance issue.

Thanks again.

Hi
Thanks for additional information. Maybe you can try using the following pseudo-code.

// Create intermadiate variable to store imported section
Section importedSection = null;
// Some loop
for (int i = 0; i < 10; i++)
{
    Section clonedSection = null;
    // If imported section is not null then just clone it.
    if (importedSection == null)
    {
        // Import section
        importedSection = doc.ImportNode(srcSection, true, ImportFormatMode.KeepSourceFormatting);
    }
    clonedSection = importedSection.Clone(true);
    // Do something with cloned section
    // .............................. 
}

Hope this could help you.
Best regards.