我使用.Net OpenXml SDK 2.0解析了一些Openxml文档。作为处理的一部分,我需要用其他句子替换某些句子。在迭代段落时,我知道什么时候我发现了一些我需要替换的东西,但是我很难理解如何替换它。如何使用OpenXML替换段落的文本Sdk
例如,假设我需要用一个html代码替换句子"a contract exclusively for construction work that is not building work."
以下面的Sharepoint可重用内容。
<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:我得到了DOCX到HTML的转换制定了使用XSLT,所以这是一种不是在这个阶段出现问题
段落节点的InnerText属性给了我正确的文本,但内部文本属性本身是不可设置的。所以 Regex.Match(currentParagraph.InnerText, currentString).Success
返回true,并告诉我当前段落包含我想要的文本。
正如我所说,InnerText本身是不可设置的,所以我尝试使用outerxml创建一个新的段落如下。
string modifiedOuterxml = Regex.Replace(currentParagraph.OuterXml, currentString, reusableContentString);
OpenXmlElement parent = currentParagraph.Parent;
Paragraph modifiedParagraph = new Paragraph(modifiedOuterxml);
parent.ReplaceChild<Paragraph>(modifiedParagraph, currentParagraph);
即使我没有太在意在这个级别的格式,它似乎并没有为已任,在outerXML似乎有打败正则表达式多余的元素。
..."16" /><w:lang w:val="en-AU" /></w:rPr><w:t>a</w:t></w:r><w:proofErr w:type="gramEnd" /> <w:r w: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>
因此,在总结,我将如何替换OPENXML与其他文本段落文本。即使以牺牲一些格式化为代价。