The problem here:
foreach (Paragraph P in D.Descendants<Paragraph>())
{
if (P.InnerText.Contains("{SomeText}"))
{
P.Remove(); //doesn't remove
}
}
Is that you are trying to remove an item from the collection while you are still iterating it. For some strange reason, the OpenXML SDK doesn't actually throw an exception here, it just silently quits the foreach
loop. Attaching a debugger and stepping through will show you that. The fix is simple:
foreach (Paragraph P in D.Descendants<Paragraph>().ToList())
{
if (P.InnerText.Contains("{SomeText}"))
{
P.Remove(); //will now remove
}
}
By adding ToList()
you are copying (shallow copy) the paragraphs to a separate list and iterating through that list. Now when you remove a paragraph it is removed from the D.Descendants<Paragraph>()
collection, but not from your list and the iteration will continue.