我正在尝试doc
从 DOCX 文件中删除变量。这是我正在使用的代码,但它不会删除任何...
这是完整的代码:
class Program
{
static void Main(string[] args)
{
string filePath = "C:\\..\\..sample.docx";
Remove removedocvars = new Remove();
removedocvars.RemoveDocVariables(filePath);
}
}
//method to remove doc vars
public void RemoveDocVariables(string fileName)
{
using (WordprocessingDocument doc = WordprocessingDocument.Open(fileName, true))
{
List<DocumentVariables> DocVarsToDelete = doc.MainDocumentPart.RootElement.Descendants<DocumentVariables>().ToList();
foreach (DocumentVariables dc in DocVarsToDelete)
{
dc.Remove();
}
doc.MainDocumentPart.Document.Save();
}
}