Welcome to MSDN Blogs Sign in | Join | Help
Using the Open XML SDK and LINQ to XML to Accept Revisions in an Open XML Wordprocessing Document

[Blog Map]

(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.)

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.

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.

Posted: Sunday, July 13, 2008 12:12 AM by EricWhite
Attachment(s): AcceptRevisions.cs

Comments

Eric White's Blog said:

In the last three posts, in addition to the information regarding how we want to alter the markup in

# July 14, 2008 12:18 AM

Julien Chable said:

Les voici : PowerTools : Utiliser System.IO.Packaging dans PowerTools pour modifier des propriétés (Doug

# July 18, 2008 4:30 AM

Eric White's Blog said:

In the next series of blog posts, I’ll be exploring some interesting aspects of SharePoint development.

# July 18, 2008 12:49 PM

Eric White's Blog said:

This post presents a custom application page in SharePoint that uses Open XML, the Open XML SDK and LINQ

# July 23, 2008 2:57 AM

Julien Chable said:

Ce post n’a pas voulu partir ni jeudi ni vendredi, le voici donc ! Des mise à jours à n’en plus finir

# August 18, 2008 3:49 AM
Leave a Comment

(required) 

(required) 

(optional)

(required) 

  
Enter Code Here: Required

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Page view tracker