Open Office xml SDK exception “Cannot insert the OpenXmlElement ”newChild“ because it is part of a tree”

大兔子大兔子 提交于 2019-12-13 02:08:10

问题


I am generating a a word document using open office sdk 2.0. I am getting an exception with is "Cannot insert the OpenXmlElement "newChild" because it is part of a tree." I am aware that is exception is due to trying duplicate nodes within the xml but I am not sure how to fix it.

private void GenerateWord(string Path)
    {
        using (WordprocessingDocument WpDoc = WordprocessingDocument.Create(Path, WordprocessingDocumentType.Document, true))
        {
            MainDocumentPart MainPart = WpDoc.AddMainDocumentPart();

            new Document(new Body()).Save(MainPart);

            Body body = MainPart.Document.Body;
            Paragraph Getskills = Test(MainPart, body);

            body.Append(Getskills);

            MainPart.Document.Save();
            WpDoc.Close();
        }
    }

    private Paragraph Test(MainDocumentPart MainPart, Body body)
    {
        Paragraph GetSkills = new Paragraph();
        string[,] ArrSkills = new string[,] { { "Live The Dream More" }, { "And Even More" } };
        List<string> SkillsList = new List<string>();

        foreach (var Item in ArrSkills)
        {
            string Skills = Item;
            SkillsList.Add(Skills);
            if (SkillsList != null)
            {
                foreach (var GetItem in SkillsList)
                {
                    GetSkills = new Paragraph(new Run(new Text(GetItem)));
                    Run BreakRun = new Run(new Break());

                    body.Append(GetSkills);
                }
            }
        }

        return GetSkills;
    }

I was just wondering if any one has come across this exception before and how they got ride of it? Thanks for any advice which you can give


回答1:


You'll need to clone the node you are getting the exception with and then insert that cloned node. See this stackoverflow answer for an example.



来源:https://stackoverflow.com/questions/25211754/open-office-xml-sdk-exception-cannot-insert-the-openxmlelement-newchild-becau

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!