Question

I have 3 tables inside the same content control, how to I get the first table and delete it?

I've tried using

SdtBlock ccWithTable = mainPart.Document.Body.Descendants<SdtBlock>().Where
                    (r => r.SdtProperties.GetFirstChild<Tag>().Val == tag).Single();

Table theTable = ccWithTable.Descendants<Table>().First();

ccWithTable.RemoveChild(theTable);

But this is giving me an error:

The specified oldChild is not a child of this element

I am suspecting that because the table isn't a child of the content control, is there any other way of doing this?

Was it helpful?

Solution

From your comment, I guess you are able to access the table object but it's not direct child ot the sdtblock, so you can't remove it with ccWithTable.RemoveChild() method (yes, it supposes the item to remove is part of direct children of this element).

But if you can access the table object (that is a sub chilld), no need to know its parent. Just call the OpenXmlElement.Remove() method: it will remove the element from its parent.

theTable.Remove();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top