There is a text in the docx document, represented in XML like this:
<w:pw:rsidR="004C461E" w:rsidRDefault="0090423A"> <w:pPr> <w:pStyle w:val="a4"/> </w:pPr> <w:rw:rsidRPr="0090423A"> <w:t>[[</w:t> </w:r> <w:proofErr w:type="spellStart"/> <w:rw:rsidR="005D17CC" w:rsidRPr="005D17CC"> <w:t>Договор.Клиент.ЮрНазвание</w:t> </w:r> <w:proofErr w:type="spellEnd"/> <w:rw:rsidRPr="0090423A"> <w:t>]]</w:t> </w:r> </w:p> It is required to replace, say [[Договор.Клиент.ЮрНазвание]] , there is a code:
var document = WordprocessingDocument.Open(path, true); string text; using (var sr = new StreamReader(document.MainDocumentPart.GetStream())) { text = sr.ReadToEnd(); } text = text.Replace("[[Договор.Клиент.ЮрНазвание]]", "ООО «Рога и копыта»"); using (var sw = new StreamWriter(document.MainDocumentPart.GetStream(FileMode.Create))) { sw.Write(text); } Of course, just a replacement will not work, between [[ and the text inside there is some markup. If you replace with regular expressions, it is unclear what combination of tags may be near inside the search text.
How to implement a replacement, similar to the one that is performed in Word itself, on the displayed text?