Using the Open XML SDK and LINQ to XML to Accept Revisions in an Open XML Wordprocessing Document

In this post, I present some code that uses the Open XML SDK and LINQ to XML to accept all revisions in an Open XML word processing document.

This blog is inactive.
New blog: EricWhite.com/blog

Blog TOC(Update September 28, 2009 - I've posted more complete code to accept tracked revisions in the PowerTools for Open XML, which is an open source project on CodePlex.)

So, for example, if we have this document:

And we run this code, the document then looks like this:

The main part of the work when accepting revisions is to delete a bunch of nodes, however, there are other tasks to accomplish.  In the attached code, you will see that in one case with inserted text, we need to move child nodes of the w:ins element up to the level of the w:ins element.  There is also extra work when joining paragraphs, removing deleted rows in a table, etc.

This code is not written in the functional style.  It is written in a procedural (imperative style) that reads the main document part into an XML tree, and then modifies the tree as appropriate to accept revisions.  This could be accomplished using the approach documented in this post, however, the added complexity of using annotations isn't really necessary for this simple example.  That approach is much more appropriate when doing something like converting a DOCX to XHTML.  In addition, I want this code to execute as quickly as possible (although it's not necessary to spend a large amount of time to optimize, in my opinion).  Using the annotations approach might slow down this code.

One note: this code does not accept tracked changes for content controls.  For instance, if you create a content control, turn on track changes, then delete the control, the document will still show that revisions exist.  Supporting this is a project for the future.  In any case, this is not a main stream scenario.

As you can see, this code is not very long - it's pretty simple.  The largest part of the code is comments documenting the changes that we want to make to the markup.  I've run this code on over 1000 documents, and it worked properly.

I initially wrote this code for the Power Tools cmdlet to accept revisions - this code is basically the same as in the Power Tools cmdlet.

Code is attached.

AcceptRevisions.cs