Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3431888
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T07:22:54+00:00 2026-05-18T07:22:54+00:00

I am parsing some Openxml word documents using the .Net OpenXml SDK 2.0. I

  • 0

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.

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.

<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 the docx to Html conversion worked out using xslt, so that is kind of not a problem at this stage

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.

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);

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" /><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>

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T07:22:55+00:00Added an answer on May 18, 2026 at 7:22 am

    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)));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using version 1.0 of Microsoft's OpenXML SDK to do some basic parsing of
Parsing some data and inserting it into 3 tables from .NET. Using Table Valued
I am using BeautifulSoup and parsing some HTMLs. I'm getting a certain data from
I'm having issues parsing some XML using DefaultHandler2. My XML takes the following form:
I am parsing some XML using Nokogiri and XPath. When I do this: doc.xpath('//Order/child::node()').each
I am parsing some XML from an RSS feed (using NSXMLParser) for a blog
I am parsing some XML with XMLSlurper (groovy 1.7.4) and I need to delete
I'm parsing some HTML using Beautiful Soup, and occasionally the HTML it returns includes
I am parsing some JSON values. I have a NSDictionary and for a certain
Am parsing some sentences (from the inaugural speech in the nltk corpus) with the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.