ArgumentException: "The node belongs to the other node"

Using the following code with the attached file, causes the exception mentioned in the title:

            var document = new Document("file.one");
            var pages = document.GetChildNodes<Page>();
            pages.ForEach(p =>
            {
                var images = p.GetChildNodes<Image>();
                images.ForEach(i =>
                {
                    p.RemoveChild(i); // The exception is thrown here, on the first iteration
                });
            });

file.zip (21.5 KB)

The same exception is thrown for the following code as well:
pages.ForEach(p =>
{
var attachedFiles = p.GetChildNodes();
attachedFiles.ForEach(a => p.RemoveChild(a));
}

I would like to replace all images and attachments in the document… I see no provision to change the data of either, so I tried removing them and re-adding them, but I cannot remove them.

@Buffer2018,

Thanks for the sample OneNote document.

You are right, there is an issue when removing images or attachments from the OneNote document. We reproduced the issue as you mentioned. We have opened the following new ticket(s) in our internal issue tracking system and will deliver their fixes according to the terms mentioned in Free Support Policies.

Issue ID(s): NOTENET-5729

You can obtain Paid Support Services if you need support on a priority basis, along with the direct access to our Paid Support management team.

@Buffer2018,

We evaluated your issue in details.

This is not an issue with the API. Since you are trying to remove the image node from page node. But the problem is that image is not always belonged to page directly and may exist as separate entity.

Please try the following sample code to cope with it, it works fine as I tested:
e.g.
Sample code:

var document = new Aspose.Note.Document("e:\\test2\\file.one");
var pages = document.GetChildNodes<Aspose.Note.Page>();
pages.ForEach(p =>
{
       var images = p.GetChildNodes<Aspose.Note.Image>();
       images.ForEach(child =>
       { 
          if (child.ParentNode == null)
          {
              return;
          }
         (child.ParentNode as Aspose.Note.OutlineElement)?.RemoveChild(child);
         (child.ParentNode as Aspose.Note.Page)?.RemoveChild(child);
       });
});

document.Save("e:\\test2\\out1.one");

Hope, this helps a bit.

Yes, that is the workaround I implemented for the time being; however, the api is very unclear about this:

  • Are images always child nodes of OutlineElement nodes? Is it safe to assume so? If not, what other types of nodes may be the parent of image nodes? I would consider adding a RemoveChild() method to the ICompositeNode interface (the type of ParentNode). This would prevent such confusion

Here is the function I created, meanwhle:

        /// <summary>
        /// Remove a child node from a parent node
        /// </summary>
        /// <param name="parent"></param>
        /// <param name="child"></param>
        public static void RemoveChild(this ICompositeNode parent, Node child)
        {
            if (parent is Page)
                (parent as Page).RemoveChild((IPageChildNode)child);
            else if (parent is Outline)
                (parent as Outline).RemoveChild((IOutlineChildNode)child);
            else if (parent is OutlineElement)
                (parent as OutlineElement).RemoveChild((IOutlineElementChildNode)child);
            else if (parent is Notebook)
                (parent as Notebook).RemoveChild((INotebookChildNode)child);
            else
                throw new ArgumentException();
        }

I think there is a mistake in your reply:

child.ParentNode cannot be both OutlineElement and Page; I think you meant:

var outlineElement = child.ParentNode as OutlineElement;
var page = outlineElement.ParentNode as Page;
outlineElement.RemoveChild(child);
page.RemoveChild(outlineElement);

Generally, images are stored as separate entries in OneNote document. Anyways, we will provide more details on it soon.

The sample code is just for demonstration purpose, you may devise your code accordingly for your requirements.

@Buffer2018,

Please note, Image is a child either of Page or OutlineElement.

@Buffer2018,

Please see the document on working with images for your reference as we have updated the document with examples.
https://docs.aspose.com/note/net/working-with-images/