I tried in different ways, Below is my code.
i am getting error as "Cannot insert the OpenXmlElement "newChild" because it is part of a tree."
public void testingExecute()
{
try
{
using (WordprocessingDocument wordDoc1 = WordprocessingDocument.Open(fromDocument1,false))
using (WordprocessingDocument wordDoc2 = WordprocessingDocument.Open(toDocument2, true))
{
BookmarkStart bookmarkStart = new BookmarkStart();
var res = from bm in wordDoc1.MainDocumentPart.RootElement.Descendants<BookmarkStart>()
where bm.Name == "TestBookmark"
select bm;
var bookmarkstart = res.SingleOrDefault();
var res1 = from bm in wordDoc1.MainDocumentPart.RootElement.Descendants<BookmarkEnd>()
where bm.Id == bookmarkstart.Id
select bm;
var bookmarkend = res1.SingleOrDefault();
body.InsertBeforeSelf<BookmarkStart>(bookmarkstart);
body.InsertAfterSelf<BookmarkEnd>(bookmarkend);
wordDoc2.MainDocumentPart.Document.Save();
}
catch (Exception Ex)
{
throw;
}
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…