gpt4 book ai didi

c# - 打开 Office xml SDK 异常 "Cannot insert the OpenXmlElement "newChild“因为它是树的一部分”

转载 作者:太空宇宙 更新时间:2023-11-03 20:03:56 24 4
gpt4 key购买 nike

我正在使用 open office sdk 2.0 生成一个 word 文档。我得到一个异常(exception)是“无法插入 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;
}

我只是想知道以前是否有人遇到过这个异常,他们是如何解决这个问题的?感谢您提供的任何建议

最佳答案

您需要克隆出现异常的节点,然后插入该克隆节点。请参阅此计算器 answer举个例子。

关于c# - 打开 Office xml SDK 异常 "Cannot insert the OpenXmlElement "newChild“因为它是树的一部分”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25211754/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com