I have a super simple document with just a table with a few blank rows and I'm trying to remove all the rows. ( The purpose is to actually remove just the blank rows ultimately but I wasn't having any luck with removing any rows at all).
public static void SearchAndReplace(string document)
{
using (WordprocessingDocument doc = WordprocessingDocument.Open(document, true))
{
var body = doc.MainDocumentPart.Document.Body;
foreach (var table in body.Elements<Table>())
{
foreach (var row in table.Elements<TableRow>())
{
table.RemoveChild<TableRow>(row);
continue;
}
}
doc.Save();
}
}
The document has a single page, with just one table with four rows but the above doesn't remove any rows for some reason.
I've also tried
row.Remove()
This has no effect either, i.e. after running this no row gets removed. Any ideas what is wrong with this? This is what my document looks like
Thanks
