如何使用OpenXML的SDK来取代段落文本 [英] How to replace an Paragraph's text using OpenXML Sdk

查看:211
本文介绍了如何使用OpenXML的SDK来取代段落文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我解析使用.net的OpenXML SDK 2.0 OpenXML的一些Word文档。我需要更换某些句子与其他句子作为处理的一部分。迭代这些段落中,我知道,当我发现我的东西需要更换,但我难倒,如何我可以取代它。

I am parsing some Openxml word documents using the .Net OpenXml SDK 2.0. I need to replace certain sentences with other sentences as part of the processing. While iterating over the paragraphs, I know when I've found something I need to replace, but I am stumped as to how I can replace it.

举例来说,可以说,我需要更换了一句专供施工未建立劳动合同。用HTML片段到Sharepoint可重用内容下文。

For example, lets say I need to replace the sentence "a contract exclusively for construction work that is not building work." with a html snippet to a Sharepoint Reusable content below.

<跨度类=MS-rtestate读取MS-reusableTextViewCONTENTEDITABLE =假的id =__ publishingReusableFragmentfragmentid =/网站/沙盒/ ReusableContent / 132_.000>合同专供施工未建设工作和LT; / SPAN>

PS:我得到了DOCX到HTML的转换制定了使用XSLT,所以这是一种不是一个问题,在这个阶段,

PS: I got the docx to Html conversion worked out using xslt, so that is kind of not a problem at this stage

段落节点的InnerText属性给了我正确的文本,但内部文本属性本身不可设置。所以     Regex.Match(currentParagraph.InnerText,currentString).Success 返回true,并且告诉我,当前段落包含我想要的文字。

The InnerText property of the Paragraph node gives me the proper text, but the inner text property itself is not settable. So Regex.Match(currentParagraph.InnerText, currentString).Success returns true and tells me that the current paragraph contains the text I want.

正如我所说的,它本身的InnerText是不可设置,所以我试图创建使用outerxml一个新的段落如下。

As I said, InnerText itself is not settable, so I tried created a new paragraph using outerxml is given below.

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

虽然我不是太在意的格式在这个级别,它似乎并没有为已任,在outerXML似乎有击败正则表达式多余的元素。

Even though I am not too concerned about the formatting at this level and it doesn't seem to have any, the outerXML seems to have extra elements that defeat the regex.

......16/&GT;&LT; W:郎W:VAL =EN-AU/&GT;&LT; / W:RPR&GT;&LT; W:T&GT; A&LT; / W:T&GT;&LT; / W :R&GT;&LT; W:proofErr W:TYPE =gramEnd/&GT; &LT; W:RW:rsidRPr =00C73B58&GT;&LT; W:RPR&GT;&LT; W:SZ W:VAL =16/&GT;&LT; W:szCs W:VAL =16/&GT;&LT ; W:郎W:VAL =EN-AU/&GT;&LT; / W:RPR&GT;&LT; W:牛逼的xml:空间=preserve&GT;合同进行建设的工作,是不是建筑的工作和LT; / W:T&GT;&LT; / W:R&GT;&LT; / W:P&GT;

..."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中的一个段落与其他的文本。即使在失去一些格式的为代价。

So in summary, how would I replace the text in a Paragraph of OpenXml with other text. Even at the expense of losing some of the formatting.

推荐答案

修正了它自己。关键是要消除所有的运行,并在当前段落创建新的运行

Fixed it myself. The key was to remove all the 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)));

这篇关于如何使用OpenXML的SDK来取代段落文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆