打开 Office xml SDK 例外"Cannot insert the OpenXmlElement "新子" because it is part of a tree"



我正在使用open office sdk 2.0生成一个word文档。我得到一个异常是"不能插入OpenXmlElement"newChild",因为它是树的一部分。"我知道这是例外是由于尝试xml内重复节点,但我不知道如何修复它。

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;
    }

我只是想知道是否有人以前遇到过这个异常,他们是如何驾驭它的?谢谢你给我的建议

您需要克隆获取异常的节点,然后插入该克隆节点。

相关内容

最新更新