Announcing the Release of the Open XML SDK Version 2 April 2009 CTP

I'm really happy to announce the release of the second CTP for version 2 of the Open XML SDK! Back in October 2008 I showed you guys an architecture diagram of the Open XML SDK. Let's take another look at this diagram:

In version 1 of the Open XML SDK we released the Open XML Packaging API, which allows you to create, open and manipulate Open XML files at the package and part level. In the first CTP of version 2 we released the Open XML Low Level DOM and Stream Reading/Writing components, which allow you to create and manipulate objects within xml parts contained in an Open XML package. In the second CTP of version 2 we are providing schema level validation functionality:

More information about the SDK and all of its components can be found here: https://msdn.microsoft.com/en-us/library/bb448854(office.14).aspx

Download Details

You can download the latest CTP for version 2 here: https://go.microsoft.com/fwlink/?LinkId=127912

Importance of Validation

If you've played around with the Open XML SDK there is a good chance at one point in time your solution has created an invalid or corrupt Open XML file. Manipulating Open XML Formats by using the Open XML Base layer makes it much easier for you to work on the Open XML files, but doing so does not guarantee the production of valid Open XML files.

The new Schema Level Validation component provides a mechanism to help you discover Open XML errors within files and in your code. This component assists you in debugging and validating Open XML files based on the schemas.

An Example

Let's say I want to create a simple document with the text "hello world." The XML snippet necessary to create this text is the following:

<w:body>

<w:p>

<w:r>

<w:t>hello world</w:t>

</w:r>

</w:p>

...

</w:body>

What happens if I forget to include the run element:

<w:body>

<w:p>

<w:t>hello world</w:t>

</w:p>

...

</w:body>

If I try to open the generated document in Word I will get the following error:

Not super helpful, right. Validating this file with .NET I get the following error message:

\word\document.xml

The element 'p' in namespace 'https://schemas.openxmlformats.org/wordprocessingml/2006/main' has invalid child element 't' in namespace 'https://schemas.openxmlformats.org/wordprocessingml/2006/main'. List of possible elements expected: 'pPr, customXml, smartTag, sdt, dir, bdo, r, proofErr, permStart, permEnd, bookmarkStart, bookmarkEnd, moveFromRangeStart, moveFromRangeEnd, moveToRangeStart, moveToRangeEnd, commentRangeStart, commentRangeEnd, customXmlInsRangeStart, customXmlInsRangeEnd, customXmlDelRangeStart, customXmlDelRangeEnd, customXmlMoveFromRangeStart, customXmlMoveFromRangeEnd, customXmlMoveToRangeStart, customXmlMoveToRangeEnd, ins, del, moveFrom, moveTo' in namespace 'https://schemas.openxmlformats.org/wordprocessingml/2006/main' as well as 'oMathPara, oMath' in namespace 'https://schemas.openxmlformats.org/officeDocument/2006/math' as well as 'fldSimple, hyperlink, subDoc' in namespace 'https://schemas.openxmlformats.org/wordprocessingml/2006/main'. (line 1, col 703)

Yes this error message is pretty useful, but the Open XML SDK schema validation component provides a bit more useful information. Here is the error message as described by the Open XML SDK:

The element has invalid child element "https://schemas.openxmlformats.org/wordprocessingml/2006/main:t". List of possible elements expected: <https://schemas.openxmlformats.org/wordprocessingml/2006/main:pPr>. Path: /word/document.xml:/w:document[1]/w:body[1]/w:p[1] OuterXml: <w:p w:rsidR="00900A7A" w:rsidRDefault="00E0086A" xmlns:w="https://schemas.openxmlformats.org/wordprocessingml/2006/main"><w:t>test</w:t></w:p>

Notice that the error gives you the exact XPath to the element and XML snippet that triggered this validation error. There are more advantages, but I will leave talking about those advantages for a future post.

Questions

Feel free to post any questions here or on the Open XML SDK MSDN forum: https://social.msdn.microsoft.com/Forums/en-US/oxmlsdk/threads. You can also post questions on the Open XML SDK Connect site: https://connect.microsoft.com/site/sitehome.aspx?SiteID=589.

Zeyad Rajabi