質問

.NET OpenXML SDK 2.0を使用して、いくつかのOpenXML Wordドキュメントを解析しています。処理の一部として、特定の文を他の文に置き換える必要があります。段落を繰り返している間、交換する必要があるものを見つけたときは知っていますが、どのように置き換えることができるかについて困惑しています。

たとえば、文を交換する必要があるとしましょう "a contract exclusively for construction work that is not building work." HTMLスニペットを使用して、以下の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:XSLTを使用してDOCXをHTMLコンバージョンに使用したので、この段階では問題ではありません。

段落ノードのInnertextプロパティは適切なテキストを提供しますが、内側のテキストプロパティ自体は設定できません。それで Regex.Match(currentParagraph.InnerText, currentString).Success真実を返し、現在の段落には私が望むテキストが含まれていることを教えてくれます。

私が言ったように、Innertext自体は和解できないので、outourxMLを使用して新しい段落を作成してみました。

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

私はこのレベルでのフォーマットについてあまり心配していませんが、それは何も持っていないように見えますが、outourxMLにはregexを倒す余分な要素があるようです。

..."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の段落のテキストを他のテキストにどのように置き換えるかを確認します。フォーマットの一部を失うことを犠牲にしても。

役に立ちましたか?

解決

自分で修正しました。重要なのは、すべての実行を削除し、現在の段落で新しい実行を作成することでした

string modifiedString = Regex.Replace(currentParagraph.InnerText, currentString, reusableContentString);
currentParagraph.RemoveAllChildren<Run>();
currentParagraph.AppendChild<Run>(new Run(new Text(modifiedString)));
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top