c# - Remove Document variables for OpenXML DOCX? -
i'm trying remove doc variables docx file. here code i'm using, doesn't remove any...
this complete code:
class program { static void main(string[] args) { string filepath = "c:\\..\\..sample.docx"; remove removedocvars = new remove(); removedocvars.removedocvariables(filepath); } } //method 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(); } }
the variables in settings.xml file, have use maindocumentpart.documentsettingspart.settings.descendants<>().
public void removedocvariables(string filename) { using (var doc = wordprocessingdocument.open(filename, true)) { doc.maindocumentpart.documentsettingspart.settings.removeallchildren<documentvariables>(); } }
Comments
Post a Comment