Last week I pointed out the open source project to create a Java library for programming against the open xml formats. Well today at TechEd we announced the release of an early preview of a managed API for the Open XML formats. It's available for download here: http://www.microsoft.com/downloads/details.aspx?FamilyId=AD0B72FB-4A1D-4C52-BDB5-7DD7E816D046&displaylang=en

This is another tool that will help make it easier for developers to build solutions on top of the Office file formats. The easier we can make it for people to build solutions, the more valuable the documents themselves become. This is a very early preview and we're hoping to get a lot of great feedback to help shape the evolution of these APIs. There is a discussion forum where you can ask questions, and provide feedback on things you'd like to see added or changed. We'll then take this feedback and use it to help generate future CTPs. You can get to the forum here: http://forums.microsoft.com/MSDN/ShowForum.aspx?ForumID=1647&SiteID=1

The goal in this first CTP was to provide some additional structure on top of what was already provided by System.IO.Packaging in .Net 3.0. Now instead of just generic parts and relationships, you actually have each part from the Open XML spec available as a strongly typed part. The API also provides package level validation so you'll know your creating all the necessary content type declarations and relationship type references.

Here's an example of how you could quickly delete a part in a spreadsheet and then validate the package:

public void ValidateSimplePackage(string xlsxFile)
{
  SpreadsheetDocument xlsxDoc = SpreadsheetDocument.Open(xlsxFile, true);
  Using(xlsxDoc) 
  {
    // Remove the main workbook part.
    xlsxDoc.DeletePart(xlsxDoc.WorkbookPart);
    // Validate the package. Will return an exception
    // because of the missing part.
    xlsxDoc.Validate(null);
  }
}

And here's an example of how you could quickly get the comments from a wordprocessing document.

public static string GetCommentsFromDocument(string document)
{
  string comments = null;
  using (WordprocessingDocument wordDoc = WordprocessingDocument.Open(document, true))
  {
    MainDocumentPart mainPart = wordDoc.MainDocumentPart;
    CommentsPart commentsPart = mainPart.CommentsPart;
    using (StreamReader streamReader = new StreamReader(commentsPart.GetStream()))
    {
      comments = streamReader.ReadToEnd();
    }
  }
  return comments;
}

I encourage everyone to take a look and let us know what you think.

-Brian