I've been quiet on the blog these past few months. I've been juggling work, a new baby (and a toddler) and some remodel plans, so extra-curricular activities like blogging have taken a back seat.

But today at TechEd 2007, we released a Community Technology Preview (CTP) of a managed-code Open XML API for use with .NET Framework 3.0!

Art Leonard and I began working on this at the 2005 PDC. He and I spent some late nights at a coffee shop in Seattle, spec'ing and prototyping the project over the past year or so. I met with a few customers to get an idea of just which scenarios are necessary. Most of this happened in the gaps of our regular work of shipping Office System 2007.

We learned a lot about working with Open XML file via the Packaging API while working with Ken Getz building the code snippets last year.  With this API, we want to reduce the number of lines of code that are required to perform simple Open XML tasks, such as;

  1. Locating the part you want 
  2. Replacing, adding or deleting parts
  3. Accessing the XML within parts (using your choice of XML editor)

The API does this by abstracting the Packaging API and by strongly typing the individual parts of the Open XML file. By exposing parts of the package s strongly-typed objects, searching for and discovering those parts will require less code.

All of the structure of the Open XML file is exposed, so you can quickly move from one part to the next within the graph and discover the part you need.

Take the WDDeleteComments() snippet. In it, we have to walk through the graph, starting at the package, find the main document part, then find the comments part, delete it and the relationships to it from the main document part:

// Get the main document part (document.xml).

foreach (System.IO.Packaging.PackageRelationship relationship in wdPackage.GetRelationshipsByType(documentRelationshipType))

{

documentUri = PackUriHelper.ResolvePartUri(new Uri("/", UriKind.Relative), relationship.TargetUri);

documentPart = wdPackage.GetPart(documentUri);

// There is only one document.

break;

}

// Delete the comments relationship. There can only be one of these.

foreach (System.IO.Packaging.PackageRelationship relationship in documentPart.GetRelationshipsByType(commentRelationshipType))

{

Uri commentUri = PackUriHelper.ResolvePartUri(documentUri, relationship.TargetUri);

PackagePart commentsPart = wdPackage.GetPart(commentUri);

documentPart.DeleteRelationship(relationship.Id);

wdPackage.DeletePart(commentUri);

// There is only one comments part.

break;

}

But with the new API, the same code is reduced to this:

using (WordprocessingDocument wordDoc = WordprocessingDocument.Open(docName, true))

{

MainDocumentPart mainPart = wordDoc.MainDocumentPart;

mainPart.DeletePart(mainPart.CommentsPart);

}

As in the snippet, you still need to go through the XML of the MainDocumentPart and remove the references to the Comments part, but I think you'll find that working with the package structure of Open XML files much is much easier.

Our goal with this CTP is to get some feedback on our work to improve the way you code solutions with Open XML using .NET.  I hope you'll download it and start playing. There is a forum on MSDN for feedback.