我在同一内容控件中有 3 个表,如何获取第一个表并将其删除?
我尝试过使用
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);
但这给了我一个错误:
The specified oldChild is not a child of this element
我怀疑因为该表不是内容控件的子级,所以还有其他方法可以做到这一点吗?
最佳答案
根据您的评论,我猜您可以访问 table
对象,但它不是 sdtblock
的直接子对象,因此您无法使用 删除它>ccWithTable.RemoveChild()
方法(是的,它假设要删除的项目是该元素的直接子元素的一部分)。
但是如果您可以访问表对象(即子对象),则无需知道其父对象。
只需调用 OpenXmlElement.Remove()
方法:它将从其父元素中删除该元素。
theTable.Remove();
关于c# - 如何在内容控制c#中获取特定表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23537133/