Replacing Text of a Bookmark in an OpenXML WordprocessingML Document

In a previous screen-cast, I showed how to retrieve the text of an OpenXML WordprocessingML document.  In this post and screen-cast, I introduce some code that enables you to replace the text of a bookmark.

This blog post and screen-cast build on the previous screen-cast.  In that screen-cast, I used an approach of ‘flattening the paragraphs’.  This is an approach of transforming the WordprocessingML to another form that is easier to work with.  This other form is not valid WordprocessingML, but it is much easier to process the w:bookmarkStart and w:bookmarkEnd elements, and the elements between them.  Then, after setting the text of the bookmark, the code then transforms the WordprocessingML back to valid markup.

This screen-cast will make more sense if you first view the previous screen-cast.  In that screen-cast, I introduce the notion of ‘flattening the paragraphs’, and discuss the algorithm.

As I have progressed in my understanding of OpenXML markup, and various approaches to processing the markup, a recurring theme is that it often is most effective to first do an intermediate transform of the markup to another form that is easier to process.  Flattening paragraphs is one of these intermediate transforms that can prove useful in a number of scenarios.  One of  my upcoming projects is to do a survey of all the ways you can do an intermediate transform of OpenXML to make it easier to implement various scenarios.

Download – Example Code