我正在尝试删除包含"{Some Text}"
. 下面的方法就是这样做的,但我注意到在删除段落后,还剩下空的段落元素。
如何以<w:p />
编程方式删除元素?
以下是我最初用来删除段落的内容。
using (WordprocessingDocument wordDoc = WordprocessingDocument.Open(file, true))
{
MainDocumentPart mainPart = wordDoc.MainDocumentPart;
Document D = mainPart.Document;
foreach (Paragraph P in D.Descendants<Paragraph>())
{
if (P.InnerText.Contains("{SomeText}"))
{
P.RemoveAllChildren();
//P.Remove(); //doesn't remove
}
}
D.Save();
}
这就是 document.xml 看起来像后记的样子:
<w:p />
<w:p />
<w:p />
<w:p />
<w:p />
<w:p />
<w:p />