我正在使用开放式办公室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;
}
我只是想知道是否有人曾经遇到过这个例外以及他们如何驾驭它?感谢您提出的任何建议