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: http://msdn.microsoft.com/en-us/library/bb448854(office.14).aspx
You can download the latest CTP for version 2 here: http://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.
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:
What happens if I forget to include the run element:
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:
The element ‘p’ in namespace ‘http://schemas.openxmlformats.org/wordprocessingml/2006/main’ has invalid child element ‘t’ in namespace ‘http://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 ‘http://schemas.openxmlformats.org/wordprocessingml/2006/main’ as well as ‘oMathPara, oMath’ in namespace ‘http://schemas.openxmlformats.org/officeDocument/2006/math’ as well as ‘fldSimple, hyperlink, subDoc’ in namespace ‘http://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 “http://schemas.openxmlformats.org/wordprocessingml/2006/main:t“. List of possible elements expected: <http://schemas.openxmlformats.org/wordprocessingml/2006/main:pPr>. Path: /word/document.xml:/w:document/w:body/w:p OuterXml: <w:p w:rsidR=”00900A7A” w:rsidRDefault=”00E0086A” xmlns:w=”http://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.
Feel free to post any questions here or on the Open XML SDK MSDN forum: http://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.