wordpress hit counter
An introduction to Open XML SDK 2.0 - OpenXML Developer - Blog - OpenXML Developer
Goodbye and Hello

OpenXmlDeveloper.org is Shutting Down

There is a time for all good things to come to an end, and the time has come to shut down OpenXmlDeveloper.org.

Screen-casts and blog posts: Content on OpenXmlDeveloper.org will be moving to EricWhite.com.

Forums: We are moving the forums to EricWhite.com and StackOverflow.com. Please do not post in the forums on OpenXmlDeveloper.org. Instead, please post in the forums at EricWhite.com or at StackOverflow.com.

Please see this blog post for more information about my plans moving forward.  Cheers, Eric

An introduction to Open XML SDK 2.0

An introduction to Open XML SDK 2.0

Rate This
  • Comments 50

Written by James Newton-King


Version 2.0 of the Open XML SDK is a big step forward for developers and brings a lot of exciting new features and tools to help work with Open XML documents.

In this article, we will to look at three new tools that are included in version 2.0 the Open XML SDK. Although the 2.0 SDK is not the final release, it works pretty well and there is certainly nothing preventing you from using the tools that are included:

  • Open XML Classes Explorer
  • Open XML Diff
  • Document Reflector


Installing SDK Tools

The Open XML SDK tools are included in the SDK download, which weighs in at about 90 megs. One thing to note when installing the SDK, is to ensure ‘Yes’ is selected when prompted whether to install the Open XML SDK Development Productivity Tools:

The tools that come with the 2.0 SDK are installed to a directory beneath the SDK install location. By default, this is Program Files -> Open XML Format SDK -> V2.0 -> Tools.


Open XML Classes Explorer

With a specification as long and as detailed as the Open XML standard it is difficult to quickly lookup documentation about the markup you are working with. Fortunately, version 2.0 of the Open XML SDK comes with the Open XML Classes Explorer.

The Classes Explorer is a fantastic tool for aiding Open XML development, combining both the new SDK document object model documentation and the Open XML standard in one application.

All document object model classes are displayed in the tree to the left. Note that the Open XML SDK Packaging classes aren’t included in this application because there is no corresponding documentation in the Ecma standard. The documentation for packaging can be found in OpenXMLSDK.chm help file, which is also included with the SDK.

For example, the screenshot above shows help for the Document class. If you are new to Open XML and want to create a Word document then this is a great place to start. In one application you can see the SDK documentation for the Document class, specification documentation for the document XML element and also what valid child content classes can be inserted beneath it.



The second tool included in the SDK, OpenXMLDiff, is all about comparing two Open XML documents and then displaying the differences.

OpenXMLDiff is simple to use, just open two Open XML documents (a source and a target) and then click Compare.

The source document.Hello world.docx, is displayed on the left and the target, Hello dave.docx, is on the right. Content removed from the source document in the target is shown in red, new content is blue and changed content is green. Non-text content that can’t be compared like images are colored gray. In the example above you can see that I have changed “Hello world!” to “Hello Dave!”, added another paragraph and added a header and footer to the page.

As well as showing files that have been added and removed, you can see OpenXMLDiff also compares content inside the XML parts that make up the Open XML document. OpenXMLDiff indents and formats the documents content before displaying it side by side, making it quite easy to read. OpenXMLDiff is also a pretty good tool just for viewing the XML content inside a document as well!


The final SDK tool is the Open XML DocumentReflector.

DocumentReflector is a really interesting application: it analyzes an existing Open XML document and then generates code to recreate the document using the new strongly typed document classes included in the Open XML SDK.

For anyone familiar with Red Gate’s free .NET Reflector program, DocumentReflector performs the same function except instead of decompiling a .NET program back to code, it is decompiling an Open XML document to code.

The DocumentReflector UI is split into a tree of document parts and the XML elements of the content inside them on the left, and the generated code on the right.

To use the DocumentReflector open a document and click on one of the hierarchy of nodes presented on the left. The DocumentReflector will then present you with C# code to generate that node within the document, ready to be copied into your .NET solution.

One of the interesting features of the tool is you can select part or even any XML element inside a part and generate code just for what you have selected.

With code already generated by the tool, it is a simple matter to copy it into your solution and start modifying it and including parameters to pass custom content to include in the final document.


public static Paragraph GenerateParagraph(string paragraphText)


  var element =

      new Paragraph(

          new Run(

              new Text(paragraphText))


  return element;



In the source code snippet above I have modified what DocumentReflector has generated to include a paragraphText parameter and made it the value of the text inserted into the paragraph. Whenever I need a simple text paragraph in a document I am generating, I can call this method and add the new paragraph to my document.


public static void Main(string[] args)


  using (WordprocessingDocument package =

    WordprocessingDocument.Create(@"c:\TestDocument.docx", WordprocessingDocumentType.Document))


    MainDocumentPart mainDocumentPart = package.AddMainDocumentPart();


    Document document = new Document(

      new Body(

        GenerateParagraph("Welcome to my test document."),

        GenerateParagraph("I generated this with help from the Open XML DocumentReflector."),

        GenerateParagraph("What a great tool!")));





While this is a very simple example, it would be easy to use DocumentReflector to create more complex scenarios which take collections of data returned from your data layer and generates tables.

DocumentReflector really simplifies the process of creating generated documents.


Open XML SDK Download

The Open XML SDK version 2.0 is currently in CTP. The latest release, which includes the new tools, is available to download here.


Page 1 of 4 (50 items) 1234