How to replace paragraph text with OpenXML Sdk

I am parsing some Openxml text documents using the .Net OpenXml SDK 2.0. I need to replace some sentences with other sentences as part of the processing. Iterating over paragraphs, I know when I found something that I need to replace, but I'm at a dead end, how can I replace it.

For example, let's say I need to replace the sentence "a contract exclusively for construction work that is not building work." on the html snippet on the Sharepoint content again.

<span class="ms-rtestate-read ms-reusableTextView" contentEditable="false" id="__publishingReusableFragment" fragmentid="/Sites/Sandbox/ReusableContent/132_.000" >a contract exclusively for construction work that is not building work.</span>

PS: I got a docx to Html conversion developed using xslt, so this is not a problem at this stage

The InnerText property in the node paragraph gives me the correct text, but the internal property of the text itself cannot be set. So Regex.Match(currentParagraph.InnerText, currentString).Success returns true and tells me that the current paragraph contains the text I want.

As I said, InnerText itself is not customizable, so I tried to create a new paragraph using outerxml below.

 string modifiedOuterxml = Regex.Replace(currentParagraph.OuterXml, currentString, reusableContentString); OpenXmlElement parent = currentParagraph.Parent; Paragraph modifiedParagraph = new Paragraph(modifiedOuterxml); parent.ReplaceChild<Paragraph>(modifiedParagraph, currentParagraph); 

Despite the fact that I'm not too concerned about formatting at this level and does not seem to have one, externalXML has additional elements that defeat the regular expression.

..."16" /><w:lang w:val="en-AU" /></w:rPr><w:t>a</w:t></w:r><w:proofErr w:type="gramEnd" /> <w:rw:rsidRPr="00C73B58"><w:rPr><w:sz w:val="16" /><w:szCs w:val="16" /><w:lang w:val="en-AU" /></w:rPr><w:t xml:space="preserve"> contract exclusively for construction work that is not building work.</w:t></w:r></w:p>

So, how can I replace the text in an OpenXml paragraph with another text. Even due to the loss of some formats.

+4
source share
1 answer

Fixed. The key was to delete all runs and create new runs in the current paragraph.

 string modifiedString = Regex.Replace(currentParagraph.InnerText, currentString, reusableContentString); currentParagraph.RemoveAllChildren<Run>(); currentParagraph.AppendChild<Run>(new Run(new Text(modifiedString))); 
+6
source

All Articles