2

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

willa
  • 629
  • 1
  • 9
  • 17
  • [Cannot insert the OpenXmlElement "newChild" because it is part of a tree](http://stackoverflow.com/questions/16320537/cannot-insert-the-openxmlelement-newchild-because-it-is-part-of-a-tree) – har07 Aug 10 '14 at 07:17

1 Answers1

5

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.

Community
  • 1
  • 1
amurra
  • 15,221
  • 4
  • 70
  • 87