Replace the entire paragraph InnerText parameter with a new string. using OpenXML C#

Viewed 76

I writing a utility that reads in a .docx template file using OpenXML in C#. The utility lists all paragraph by its heading and depending on the paragraph heading (configurable via a ini file) I have the option of editing the paragraph or removing the paragraph completely. I have everything working with the removing of the paragraph but I'm struggle to be able to replace all the paragraph text with a new one. I know about the below code but this doesn't do what I want because paragraph comes in many elements and I'm struggling to work out how I can replace the paragraph text. The paragraph text does come in its entirety in paragraph.InnerText but this cannot be edited. Is there a workaround? Thanks in advance.

foreach (Paragraph paragraph in paragraphs)
{
    foreach (Run run in paragraph.Elements<Run>())
    {
        foreach (Text text in run.Elements<Text>())
        {
            Console.WriteLine (text.Text);
        }
    }
}
1 Answers

With a little help from another post I've solved my issue. It was quite simple in the end I just had to added the folllowing.

 paragraph.RemoveAllChildren<Run>();
 paragraph.AppendChild<Run>(new Run(new Text("New Paragraph Text")));
Related