Article by Sheela E.N, Sonata Software Limited
This article explains about changes made in Open XML SDK April 2008 CTP compared to Open XML SDK June 2007 CTP
Open XML SDK provides a set of strongly typed classes to create and consume Office Open XML based documents (i.e Word 2007, Excel 2007 and PowerPoint 2007). This has classes and methods for simplifying developer work within parts. This SDK is based on System.IO.Packaging API which is part of .Net 3.0.
Changes in Open XML SDK April 2008 CTP
1. AddNewPart<T>() method moved to OpenXmlPartContainer Class from Class OpenXmlPart.
The Open XML Format SDK June 2007 CTP provides an OpenXMLPart class that enables to add a known part at a package level through fixed methods such as AddCoreFilePropertiesPart() and AddExtendedFilePropertiesPart(). The Open XML Format SDK April 2008 CTP moves the AddNewPart<T>() method from the OpenXMLPart class to the OpenXMLPartContainer class.
Code sample :
WordprocessingDocument doc = WordprocessingDocument.Create(@"d:\....\aa.docx", WordprocessingDocumentType.Document);
MainDocumentPart mainpart = doc.AddMainDocumentPart();
XmlDocument test = new XmlDocument();
test.Load(@"..\..\document.xml");
test.Save(mainpart.GetStream());
CoreFilePropertiesPart part1 = doc.AddNewPart<CoreFilePropertiesPart>();
test.Load(@"..\..\core.xml");
Benefit: With this move, the Open XML API enables to add any part on the package with a generic call; therefore, the coding is made easier.
Impact: No impact to the existing code which is based on June 2007 CTP, since it is a new feature
2. AddNewPart<T>() that Receives a Relationship Id Parameter as Input
In Open XML Format SDK June 2007 CTP , AddNewPart method used to generate a random relationship ID. If we had already an xml file for part, with fixed relationship id, then it was developer’s work to replace it with newly generated relationship id. In April 2008 CTP, this method takes relationship Id as parameter, instead of generating a random number.
Code sample:
HeaderPart headerpart = mainpart.AddNewPart<HeaderPart>("rid1");
If the value of the Id parameter is null, the Open XML API generates a new Id value for the OpenXMLPart. If the Id already exists, the Open XML API throws a duplicate ID exception.
Benefit: Code of replacing relationship id from part is eliminated
Impact: No impact to the existing code which is based on June 2007 CTP, since it is a new feature
3. Support Annotations on Parts
We can associate arbitrary data with a particular part of the package. once having read the XML from the part, we can add the same (object) instance as an annotation on the part, We can easily retrieve the annotation instead of rereading the XML each time we need to access it. Annotations allows to associate any object with an OpenXmlPartContainer (the base class of OpenXmlPart) in a type safe way.
Code sample:
XDocument xdoc = part.Annotation<XDocument>();
if (xdoc != null)
{
return xdoc;
}
using (StreamReader streamReader =
new StreamReader(part.GetStream()))
xdoc = XDocument.Load(XmlReader.Create(streamReader));
//for example, part is main document part
part.AddAnnotation(xdoc);
Benefit: This will Minimize Serialization and Deserialization of the part content.
4. The following three Document Enumerations are moved to a Different Namespace. The Open XML Format SDK June 2007 Technology Preview has three key enumerations that are part of the Microsoft.Office.DocumentFormat.OpenXml.Packaging namespace:
· WordprocessingDocumentType
· SpreadsheetDocumentType
· PresentationDocumentType
The Open XML Format SDK April 2008 Technology Preview moves the previous enumerations to the Microsoft.Office.DocumentFormat.OpenXml namespace
Code sample :
using Microsoft.Office.DocumentFormat.OpenXml.Packaging;
using Microsoft.Office.DocumentFormat.OpenXml;
WordprocessingDocument doc = WordprocessingDocument.Create(@"c:\test\test.docx", WordprocessingDocumentType.Document);
Impact: This is a breaking change and has an impact on existing June 2007 CTP code.
5. Change in class names
The Open XML Format SDK April 2008 CTP renames few classes. Below is the list of classes undergone changes in names.
|
Open XML Format SDK June Technology Preview |
Open XML Format SDK April Technology Preview |
|
CommentsPart |
WordprocessingCommentsPart |
|
DiagramLayoutPart |
DiagramLayoutDefinitionPart |
|
EmbeddedControlPart |
EmbeddedControlPersistencePart |
|
EmbeddedControlPartType |
EmbeddedControlPersistencePartType |
|
PivotCachexxx such as:
· PivotCacheDefinitionPart
· PivotCacheRecordsPart
· PivotCacheDefinitionPart.PivotCacheRecordsPart
PivotCacheRecordsPart.PivotCacheRecordsPart |
PivotTableCachexxx such as:
· PivotTableCacheDefinitionPart
· PivotTableCacheRecordsPart
· PivotTableCacheDefinitionPart.PivotTableCacheRecordsPart
PivotTableCacheRecordsPart.PivotTableCacheRecordsPart |
|
OpenXmlPart.TargetExt |
OpenXmlPart.TargetFileExtension |
Benefit: Code is easier to understand
Impact: This is a breaking change and has an impact on existing June 2007 CTP code.