我认为以下内容可行:
var bod = wordDoc.MainDocumentPart.Document.Body;
foreach (var bookmark in bod.Descendants<BookmarkStart>())
{
bookmark.Remove();
}
foreach (var bookmark in bod.Descendants<BookmarkEnd>())
{
bookmark.Remove();
}
但这会破坏文件。
答案 0 :(得分:5)
试试这个,它适用于我的文档。
var bs = wordDoc.MainDocumentPart.Document
.Descendants<BookmarkStart>()
.ToList();
foreach (var s in bs)
s.Remove();
var be = wordDoc.MainDocumentPart.Document
.Descendants<BookmarkEnd>()
.ToList();
foreach (var e in be)
e.Remove();
答案 1 :(得分:0)
尝试添加
bookmark.RemoveAllChildren();
之前
bookmark.Remove()
答案 2 :(得分:0)
简单的解决方案:
wordDoc.MainDocumentPart.Document.RemoveAllChildren<BookmarkStart>();
wordDoc.MainDocumentPart.Document.RemoveAllChildren<BookmarkEnd>();