c# - 如何在内容控制c#中获取特定表

标签 c# asp.net openxml openxml-sdk contentcontrol

我在同一内容控件中有 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/

相关文章:

javascript - 如何在 JavaScript 中使用 XPath 从命名空间的 OOXML 中进行选择?

c# - 修改excel单元格

c# - 错误 : No value given for one or more required parameters

c# - 如何在 C# 中轻松排序 XML 文件?

c# - WCF 和 Windows 身份验证

.net - <%# 和 <%= 开始标签有什么区别?

javascript - Ooxml 中不显示下划线

c# - 如何使用 ASP.NET 制作动态标签?

c# - 从 Windows 服务访问数据库

c# - 将 String、Float 和 DateTime 值插入 sql 表