Upload
vothuy
View
223
Download
1
Embed Size (px)
Citation preview
Hands-On LabLab Manual
OpenXML Markup Compatibility and Extensibility with the OpenXML SDK 2.0
Information in this document, including URL and other Internet Web site references, is subject to change without notice. This document supports a preliminary release of software that may be changed substantially prior to final commercial release, and is the proprietary information of Microsoft Corporation.
This document is for informational purposes only. MICROSOFT MAKES NO WARRANTIES, EITHER EXPRESS OR IMPLIED, AS TO THE INFORMATION IN THIS DOCUMENT.
The entire risk of the use or the results from the use of this document remains with the user. Complying with all applicable copyright laws is the responsibility of the user. Without limiting the rights under copyright, no part of this document may be reproduced, stored in or introduced into a retrieval system, or transmitted in any form or by any means (electronic, mechanical, photocopying, recording, or otherwise), or for any purpose, without the express written permission of Microsoft Corporation.
Microsoft may have patents, patent applications, trademarks, copyrights, or other intellectual property rights covering subject matter in this document. Except as expressly provided in any written license agreement from Microsoft, the furnishing of this document does not give you any license to these patents, trademarks, copyrights, or other intellectual property.
Unless otherwise noted, the example companies, organizations, products, domain names, e-mail addresses, logos, people, places and events depicted herein are fictitious, and no association with any real company, organization, product, domain name, email address, logo, person, place or event is intended or should be inferred.
© 2009 Microsoft Corporation. All rights reserved.
Microsoft, MS-DOS, Windows, Windows NT, Windows Server, Visual C# and Visual Studio are either registered trademarks or trademarks of Microsoft Corporation in the United States and/or other countries.
The names of actual companies and products mentioned herein may be the trademarks of their respective owners.
Contents
MARKUP COMPATIBILITY AND EXTENSIBILITY WITH THE OPEN XML SDK 2.0............................................2Lab Prerequisites................................................................................................................................................. 2Lab Objective....................................................................................................................................................... 3Exercise 1 - Introduction to Markup Compatibility and Extensibility (15 Minutes).................................................3
Task 1 - Ignorable Name Spaces..................................................................................................................... 3Task 2 – Extension Lists (extLst)..................................................................................................................... 6Task 3 – Alternate Content Blocks.................................................................................................................13
Exercise 2 – Processing MCE Content with the Open XML SDK (15 Minutes)..................................................17Task 1 – Pre-process an Open XML Document for Office 2007 and Office 2010..........................................17Task 2 – Parse an Alternate Content Block choosing based on URI.............................................................23
Exercise 3 – Creating MCE Content using the Open XML SDK (15 Minutes)....................................................25Task 1 – Create a PresentationML document with an Alternate Content Block (ACB) using the Open XML SDK................................................................................................................................................................ 25Task 2 – Create a SpreadsheetML document with an Extension List using the Open XML SDK..................28
Exercise 4 – Open XML Validation using the Open XML SDK (10 Minutes)......................................................31Task 1 – Create a simple console application to validate documents............................................................31Task 2 – Test validation by introducing schema based and semantic errors.................................................32
Page 1
Markup Compatibility and Extensibility with the Open XML SDK 2.0Estimated time to complete this lab: 1 hr
Copy Walkthrough project to your file system. The location of the files will hereafter be referred to as <source directory>.
Lab PrerequisitesThis lab requires the following prerequisites, please check through the following instructions to make sure each of them are in place.
Download Open XML SDK 2.0 for Microsoft Office (December 2009 Community Technical Preview)http://www.microsoft.com/downloads/details.aspx?FamilyId=C6E744E5-36E9-45F5-8D8C-331DF206E0D0&displaylang=en
Setup your browser to be default program for opening XML files or use the XML editor of your choice
Access to Office 2007 and Office 2010. Both are required to allow comparisons as to how each handles the MCE content.
Page 2
Lab Objective
The Markup Compatibility and Extensibility specification contained in part 3 of the IS29500 Open XML specification provides a mechanism for implementers to add and interpret their own markup to an Open XML document while maintaining backward compatibility with existing clients.In this lab you will learn about Markup Compatibility and Extensibility in the context of the relationship between Microsoft Office 2010 and Microsoft Office 2007. You will see how Office 2010 emits certain MCE constructs to provide up-level constructs for Office 2010 and legacy constructs for Office 2007.You will then look at how the Open XML SDK 2.0 helps support working with MCE by pre-parsing MCE constructs based on a given Microsoft Office version.
Exercise 1 - Introduction to Markup Compatibility and Extensibility (15 Minutes)In this exercise you will create a variety of documents in Office 2010 and Office 2007 and examine the markup that they generate. You will use this exercise to learn the basic MCE constructs and which of these are likely to be round tripped through certain client applications.
Task 1 - Ignorable Name Spaces
In this task, you will:a) Create a WordprocessingML document in Microsoft Word 2010 containing a feature not
supported in Microsoft Word 2007b) Inspect XML produced and how it is structured so any compliant markup consumer is able read
the content
1. Open Microsoft Office Word 20102. In the blank document type in Glowing Text.
3. Select the text then from the ribbon bar go to Home | Text Effects | Glow – Accent 6
Figure 1 - Appling glow effect
Page 3
4. Save document as Glowing Text.docx and close
5. Open document up in Microsoft Word 2007
Note: The text in Microsoft Word 2007 no longer has the glow effect
Figure 2 - Differences between Microsoft Word 2007 and 2010 in rendering glowing text
6. Close document then navigate to where document is located right click on document select rename to Glowing Text.zip
Figure 3 - Rename Glowing Text.docx to zip file
7. Click Yes
8. Double Click on Glowing Text zip - navigate to and open /word/document.xml.
Figure 4 – Glowing Text OpenXml PackagePage 4
9. Locate the glow declaration in the xml file, note it is declared in the w14 namespace
Figure 5- Glowing Text XML output
10. At the top of the document the w14 and wp14 namespaces are declared as ignorable
Figure 6 - XML ignorable namespaces
The ignorable attribute specifies which XML namespaces encountered in a markup file may be ignored by a markup consumer. During processing if a markup consumer encounters an element or attribute in a non-understood and ignorable namespace, the markup consumer shall treat that element or attribute as it if did not exist and shall not generate an error.
The ignorable namespace enables document markup produces to emit their own namespaces into documents and ensure any compliant markup consumer is still able to read the content
For further Reading download ISO 29500 Part 4
http://www.iso.org/iso/iso_catalogue/catalogue_tc/catalogue_tc_browse.htm?commid=45374
Page 5
Task 2 – Extension Lists (extLst)
In this task, you will:a) Create a PresentationML document in Microsoft PowerPoint 2010 containing a feature not
supported in Microsoft Office PowerPoint 2007b) Round trip an extension list through PowerPoint 2010 and PowerPoint 2007.
1. Open Microsoft Office PowerPoint 2010
2. Insert | Picture
3. Select image from resources folder Navigate to and select <source subdirectory>/Images/jelly.wdp Note: Change file type selection to All Files *.*
Figure 7 - Insert jellyfish image
Page 6
4. Click Insert
Figure 8 - Jellyfish image inserted into document
5. Add Glow Edges effectNote: This effect is specific to Microsoft PowerPoint 2010
From ribbon select Format | Artistic Effects | Glow Edges
Figure 9 - Add glowing edges to image
Page 7
6. Image should appear as below Save As Jellyfish.pptx
Figure 10 - Image with glowing edges applied
7. Open Jellyfish.pptx in Microsoft PowerPoint 2007
Note: Even though Microsoft PowerPoint 2007 doesn’t have the glow edges effect we still have a representation of the image; the reason for this is that Microsoft PowerPoint 2010 has emitted a processed jpeg into the OpenXML package. We don’t have the ability to work with the artistic in PowerPoint 2007, but, the effect is still visible as it has been encoded into the jpeg.
Page 8
8. Click image to select
9. From ribbon bar select Format | Picture Styles | Beveled Oval Black
Figure 11 - Apply rounded border
Note: To select the oval border you may need to use the scroll bar located on the right hand side of the border selection to navigate down a row.
Figure 12 - Image with rounded border applied
10. Save and Close Document
11. Open Jellyfish.pptx back up in PowerPoint 2010
12. Click image to select
13. From ribbon select Format | Artistic Effects | choose any effect you wish
Note: Not only have the changes made in 2007 been taken back up to 2010 we can also still work with the original underlying image with its artistic effect applied.
14. Undo artistic effect applied in step 13 save and closeNavigate to where Jellyfish.pptx is saved and rename to Jellyfish.zip
Figure 13 - Rename Jellyfish.docx to .zip
Page 9
15. Click Yes
16. Double click on Jellyfish.zip navigate to and open /ppt/slides/slide1.xml
Figure 14 - Open slide xml file
Page 10
17. Find the blip sectionNote: We can see the blip is referencing a piece of content <a:blip r:embed=”rId2” located in the media directory
Figure 15 - XML output from jellyfish slide
18. Navigate to and open /ppt/media folder in the Jellyfish.zip package
Figure 16 - Media files in Jellyfish Open XML package
19. Open both images
Page 11
The media folder contains two files, the first hdphoto1.wdp which is the original Jellyfish file and image1.png which is a flattened image created by PowerPoint 2010 with the artistic effect applied to it
The markup output uses the flattened image <a:blip r:embed=”rId2”> as the primary representation, then it has emitted an extension list into the document. When Office 2010 opens the document it understands the extension list and realizes it shouldn’t be using the flattened image it should actually be displaying its own version and then applying the artistic effectWhen we round tripped the document, Office 2007 was working with the blip (<a:blip r:embed=”rId2”>) where as Office 2010 understands the extension list Uri and its then able to work with the original image and apply the artistic effects.
Page 12
Task 3 – Alternate Content Blocks
In this task, you will:a) Create an alternate content block
1. Open Microsoft Office Word 2010
2. From the ribbon bar select Insert | Shapes | Rectangle
Figure 17 - Insert rectangle
3. Right click on rectangle select Add Text, enter text of your choice
Figure 18 - Add text to rectangle
Page 13
4. From ribbon bar select Format | Shape Effects | Glow | Blue, 18 pt glow accent color 1
Figure 19 - Add glowing edges to rectangleNote: Format tab only visible if rectangle selected
5. Save document as Glowing Rectangle.docx and close
6. Save a copy of this version of the document as you will use it in later tasks
7. Navigate to Glowing Rectangle.docx and rename to Glowing Rectangle.zip
Figure 20 - Rename Glowing Rectangle.docx to .zip
Page 14
8. Double click on Glowing Rectangle.zip navigate to and open /word/document.xml Find the alternate content block.
We have a choice which requires the content consumer understand the wps namespace which is the Word 2010 word processing shape namespace then we have a fallback option which is used if the content consumer does not understand the required wps namespace
Figure 21 - Alternate content block
9. Close Page
Page 15
10. Rename the file back to Glowing Rectangle.docx and open up in Microsoft Office Word 2007
Figure 22 - Drawning representation outputted by Word 2007Note: The glowing edges no longer rendered
11. Press the spacebar once and resave the document – (do not edit the rectangle).
12. Close file and rename to Glowing Rectangle.zip
13. Double click on Glowing Rectangle.zip navigate to and open /word/document.xml
Figure 23 - Glowing Rectangle xml output after saved from Word 2007
We can see the alternate content block no longer exists despite the fact we didn’t actually edit any of the content in the alternate content block. It is the Microsoft Word 2007 implementation choice to remove the alternate content block choices on save of the document regardless of whether an edit has been made of the specific content within that alternate content block.
Page 16
14. Rename file back to Glowing Rectangle.docx and open up in Microsoft Office Word 2010Since the alternate content block no longer exists Word 2010 is working with the down rev VML representation
An Alternate content block stores multiple representations of the same content, each within its own choice block, it is used in situations where a consuming application may not be capable of interpreting what a producing application wrote. A markup consumer can interrogate each choice element selecting the one it understands or the fallback option if it doesn’t understand the namespaces required.
For further reading download ISO 29500 Part 4
http://www.iso.org/iso/iso_catalogue/catalogue_tc/catalogue_tc_browse.htm?commid=45374
Exercise 2 – Processing MCE Content with the Open XML SDK (15 Minutes)In this exercise you will learn how to use the pre-processing capability of the Open XML SDK to preprocess documents based on a particular version of Microsoft Office. Pre processing a document will select the appropriate content from within the compatibility markup based on a specific consumer profile and retain only that content in the document. By taking advantage of the pre-processing capability of the Open XML SDK you can obviate the need to examine each of the alternate content block choices and determine which to process yourself.
Task 1 – Pre-process an Open XML Document for Office 2007 and Office 2010
In this task, you will:a. Create a windows application to read and pre-process a Microsoft Office documentb. Inspecting xml output after pre-processing and comparing differences when targeting pre-
processing for Office 2007 and 2010.
All code to be written or inserted in is to be placed underneath preceding line in descending order unless otherwise stipulated.
1. Open Microsoft Visual Studio 2. Open <source directory>\Walkthrough.sln
3. Open MCEWalkthrough.cs
4. Locate PreProcessingOfficeDocument method
5. Inside PreProcessingOfficeDocument create two variables called office2007XMLOutput and office2010XMLOutput of type string these will store the output from the processed content.
6. Create new instance of the OpenSettings class called osOpenSettings os = new OpenSettings();
7. Create a new instance of MarkupCompatibilityProcessSettings class called markupSettings2007 setting its constructor parameters to process all parts in the package and target file format version of Office 2007
MarkupCompatibilityProcessSettings markupSettings2007 = new MarkupCompatibilityProcessSettings(
Page 17
MarkupCompatibilityProcessMode.ProcessAllParts, FileFormatVersions.Office2007);
8. Create another instance of MarkupCompatibilityProcessSettings class called markupSettings2010 setting its constructor parameters to process all parts and target file format version of Office 2010
MarkupCompatibilityProcessSettings markupSettings2007 = new MarkupCompatibilityProcessSettings( MarkupCompatibilityProcessMode.ProcessAllParts, FileFormatVersions.Office2010);
9. Set the MarkupCompatibilityProcessSettings parameter of the newly created os object to markupSettings2007 object
os.MarkupCompatibilityProcessSettings = markupSettings2007;
10. Insert code
using (WordprocessingDocument myDoc = WordprocessingDocument.Open(filepath, false, os))
{ office2007XMLOutput = myDoc.MainDocumentPart.Document.InnerXml; }
11. Set the os objects MarkupCompatibilityProcessSettings parameter to markupSettings2010
os.MarkupCompatibilityProcessSettings = markupSettings2010;
12. Insert Codeusing (WordprocessingDocument myDoc = WordprocessingDocument.Open(filepath, false,
os)) { office2010XMLOutput = myDoc.MainDocumentPart.Document.InnerXml;
}
Page 18
Figure 24 - Completed method PreProcessingOfficeDocument
13. Place a break point at the end of the PreProcessingOfficeDocument method
Figure 25 - PreprocessingOfficeDocument with break point
14. Overwrite static void Main method with code below
static void Main(string[] args){PreProcessingOfficeDocument("PreProcessContent.docx"); }
Page 19
If you open up PreProcessContent.docx located in the projects debug folder you will see it’s a replica of the document created in exercise 1 task 3 “Glowing Rectangle.docx”
15. Click Debug | Start Debugging16. When application reaches breakpoint right click on office2007XMLOutput variable | Add Watch
Figure 26 - Add watch
17. Repeat for office2010XMLOutput variable
18. Open Watch Tab19. Click on arrow next to magnifying glass in value field for office2010XMLOutput select XML
Visualizer
Figure 27 - Watch tab properties
Page 20
Figure 28 - Office 2010 preprocessed content
We can see the 2010 preprocessed content is using the drawing tag which if you recall from exercise 1 task 3 the drawing tag was declared inside the Alternate Content Block choice element. This element required the markup consumer understand the wps namespace which is the Office 2010 word processing shape namespace
20. Click Close
Page 21
21. Repeat step 19 for office2007XMLOutput.
Figure 29 - Office 2007 preprocessed content
Inspecting the XML output we can see the 2007 preprocessed content has no reference to the drawing tag as it does not understand the wps namespace instead displaying the fallback option from the Alternate Content Block.
22. Click Close23. Press Shift + F5 to stop debugging
We have seen how to use the OpenXML SDK to preprocess content to return only the compatibility markup we are interested in removing the need to manually inspect an Alternate Content Block choices ourselves and decided which to process.
Technical Referencehttp://msdn.microsoft.com/en-us/library/documentformat.openxml.packaging.markupcompatibilityprocesssettings(office.14).aspx
Page 22
Task 2 – Parse an Alternate Content Block choosing based on URI
In this task, you will: a. Inspect a WordProcessingML document retrieving the alternate Content blockb. Parse an alternate content block inspecting the choice elements and choosing the appropriate
one
1. Locate ParseACB Method
2. Underneath the Todo comment create a variable called choices of type IEnumerable<AlternateContentChoice> to populate this variable retrieve all AlternateContentChoice elements from the alternateContentBlock variable.
IEnumerable<AlternateContentChoice> choices = alternateContentBlock.Elements<AlternateContentChoice>();
3. An alternate content block doesn’t only consist of choices it also has a fallback option; we need to retrieve this as well. Create a variable called fallback of type AlternateContentFallback; populate this variable by retrieving the first element of type AlternateContentFallback located in alternateContentBlock
AlternateContentFallback fallback = alternateContentBlock.Elements<AlternateContentFallback>().First();
Note: We retrieve only the first AlternateContentFallback because an alternate content block can only have one fallback option.
4. Create a foreach loop
foreach (AlternateContentChoice choice in choices) {
}
5. Inside the foreach loop create an if statement that checks if the current choice requires the wps namespace be understood
if (choice.Requires == "wps"){
}
6. Inside the if statement paste the three Console.WriteLine lines below
Console.WriteLine("Choice requires wps");Console.WriteLine("Press enter to exit");Console.ReadKey();
7. Overwrite static void Main method with:static void Main(string[] args)
Page 23
{ParseACB("PreProcessContent.docx");}
8. Press F59. When we execute our code we can see the Alternate Content Block choice requires the wps
namespace
Figure 30 - Ex 2 Task 2 Output
10. Navigate to <source directory>\bin\debug\PreProcessContent.docx. Using techniques learnt in earlier exercises open up the xml package, navigate to and open \word\document.xml
11. Locate the choice element; we can see that this requires the wps namespace as discovered in our application.
Note: In our method we inspected the AlternateContentChoice Requires parameter to see if the wps value was present. For this to be effective in a real world scenario it would need be extended to make sure your markup consumer understands all values contained in the Requires parameter not just the wps namespace as Requires can hold multiple values and a markup consumer must understand all namespaces to select that choice and process the content. If we didn’t understand all the required namespaces we would process the fallback option
Technical Referencehttp://msdn.microsoft.com/en-us/library/documentformat.openxml.alternatecontent(office.14).aspx
Page 24
Exercise 3 – Creating MCE Content using the Open XML SDK (15 Minutes)In this Exercise you will learn how to use the Open XML SDK to emit some of the MCE markup that you learnt about in Exercise 1
You will create a simple PresentationML document that includes an Alternate Content Block. This will specify a slide transition using the new Office 2010 ‘Vortex’ transition effect as well as a Fallback choice for clients that do not understand this namespace
You will then create a SpreadsheetML document that will include an Extension List to apply different Icon Sets.
Task 1 – Create a PresentationML document with an Alternate Content Block (ACB) using the Open XML SDK
In this task, you will:a. Create an alternate content blockb. Create an alternate content choice and fallback optionc. Insert alternate content block into an existing file
1. Locate ADDACBToPresentationML method.
2. Underneath the Todo comment declare a new instance of the AlternateContent Class called alternateContent
AlternateContent alternateContent = new AlternateContent();
Note: AlternateContent class when serialized out as XML will render as the mc:AlternateContent tag
3. Declare a new instance of the AlternateContentChoice Class called alternateContentChoice, setting its Requires parameter to p14
AlternateContentChoice alternateContentChoice = new AlternateContentChoice() { Requires = "p14" };
Tip: Hover the mouse over an OpenXml object to display what tag the object will be rendered as once serialized as XML
Figure 31 - OpenXML object description
4. Declare a new instance of the AlternateContentFallback Class called alternateContentFallback
AlternateContentFallback alternateContentFallback = new AlternateContentFallback();
Note: We have now created all the classes required for an alternate content block in our package. An alternate content parent class, a choice and a fallback.
Page 25
5. Declare a new instance of Transition Class called choiceTransition setting its Speed parameter to slow and Duration to 4000
Transition choiceTransition = new Transition() { Speed = TransitionSpeedValues.Slow, Duration = "4000" };
6. Declare a new instance of Transition Class called fallBackTransition setting its speed parameter to slow
Transition fallBackTransition = new Transition() { Speed = TransitionSpeedValues.Slow };
7. Declare a new instance of FadeTransition class,
FadeTransition fadeTransition = new FadeTransition();
8. Declare a new instance of VortexTransition class setting its Direction property to TransitionSlideDirectionValues.Right
P14.VortexTransition vortexTransition = new P14.VortexTransition() { Direction = TransitionSlideDirectionValues.Right };
9. Insert code
Timing timing = new Timing();TimeNodeList timeNodeList = new TimeNodeList();
ParallelTimeNode parallelTimeNode = new ParallelTimeNode();CommonTimeNode commonTimeNode = new CommonTimeNode(){ Id = (UInt32Value)1U, Duration = "indefinite", Restart = TimeNodeRestartValues.Never, NodeType = TimeNodeValues.TmingRoot};
10. Append newly created objects into slide using the following code
choiceTransition.Append(vortexTransition); fallBackTransition.Append(fadeTransition); alternateContentChoice.Append(choiceTransition); alternateContentFallback.Append(fallBackTransition); if (!corruputFile) { alternateContent.Append(alternateContentChoice); } alternateContent.Append(alternateContentFallback); parallelTimeNode.Append(commonTimeNode); timeNodeList.Append(parallelTimeNode); timing.Append(timeNodeList); slide.Append(alternateContent); slide.Append(timing);
The diagram below represents what we just created. We inserted a single transition object into our alternate content blocks choice and fallback element. A transition object needs an
Page 26
associated transition effect child element, we created two; vortex (Office 2010 transition) and Fade a generic transition. A slide also needs a timing child element to moderate the amount of time a transition is run from beginning to end; this is what we created in step 9. In the final step we appended each of the elements we created throughout the task in the correct order.
Timing TimeNodeList ParallelTimeNode CommonTimeNodeSlide
ACB Choice Transition Transition Effect (Vortex)
Fallback Transition Transition Effect (Fade)
11. Overwrite static void Main method with below:static void Main(string[] args){ADDACBToPresentationML("AlternateContentBlock.pptx", "AlternateContentBlock-Created.pptx", false);}
12. Press F5 to run
13. Navigate to the <source directory>\bin\debug folder
14. Rename AlternateContentBlock-Created.pptx to AlternateContentBlock-Created.zip15. Double click AlternateContentBlock-Created.zip and navigate to and open \ppt\slides\
slide2.xml16. Find the AlternateContent element
Figure 32 - Ex 3 Task 1 Output
Note: We can see the new alternate content element we added with the alternate content choice containing the Office 2010 vortex transition effect and the fade transition fallback option.
17. Open presentation in Office 2007 and 2010 to view alternate content block in actionPage 27
Task 2 – Create a SpreadsheetML document with an Extension List using the Open XML SDK
In this task, you will:a. Add an extension list to a SpreadsheetML documentb. Work with the Office 2010 namespace to create an extension list
1. Locate the AddExtensionListToSpreadsheet method
2. Underneath the //Todo comment declare new instance of ConditionalFormattings class called conditionalFormattings
X14.ConditionalFormattings conditionalFormattings = new X14.ConditionalFormattings();
Note: This class along with the classes you are creating in the next 5 steps resides within the DocumentFormat.OpenXml.Office2010.Excel namespace. This has been given the alias X14 so to avoid ambiguous references between itself and the DocumentFormat.OpenXml.Spreadsheet namespace
3. Declare new instance of X14.ConditionalFormatting class called conditionalFormatting and a new instance of X14.ConditionalFormattingRule called conditionalFormattingRuleX14.ConditionalFormatting conditionalFormatting = new X14.ConditionalFormatting();
X14.ConditionalFormattingRule conditionalFormattingRule = new X14.ConditionalFormattingRule();
conditionalFormattingRule.Type = ConditionalFormatValues.IconSet;conditionalFormattingRule.Priority = 1;conditionalFormattingRule.Id = "{E89BCF05-C1AD-4D11-A785-C41CF4A3163B}";
4. Declare new instance of X14.IconSet class called iconSet setting its IconSetTypes property to X14.IconSetTypeValues.ThreeStars :X14.IconSet iconSet = new X14.IconSet();iconSet.IconSetTypes = X14.IconSetTypeValues.ThreeStars;
5. Declare a new instance of X14.ConditionalFormattingValueObject class called conditionalFormattingValueObject1 and a new instance of Formula class called formula1
X14.ConditionalFormattingValueObject conditionalFormattingValueObject1 = new X14.ConditionalFormattingValueObject();conditionalFormattingValueObject1.Type = X14.ConditionalFormattingValueObjectTypeValues.Percent;
Excel.Formula formula1 = new Excel.Formula();formula3.Text = "67";
Page 28
6. Declare a new instance of X14.ConditionalFormattingValueObject class called conditionalFormattingValueObject2 and a new instance of Formula class called formula2
X14.ConditionalFormattingValueObject conditionalFormattingValueObject2 = new X14.ConditionalFormattingValueObject();conditionalFormattingValueObject2.Type = X14.ConditionalFormattingValueObjectTypeValues.Percent;
Excel.Formula formula2 = new Excel.Formula();formula3.Text = "67";
7. Declare a new instance of X14.ConditionalFormattingValueObject class called conditionalFormattingValueObject3 and a new instance of Formula class called formula3
X14.ConditionalFormattingValueObject conditionalFormattingValueObject3 = new X14.ConditionalFormattingValueObject();conditionalFormattingValueObject3.Type = X14.ConditionalFormattingValueObjectTypeValues.Percent;
Excel.Formula formula3 = new Excel.Formula();formula3.Text = "67";
8. Declare a new instance of Excel.ReferenceSequence class called referenceSequence
Excel.ReferenceSequence referenceSequence = new Excel.ReferenceSequence();referenceSequence.Text = "A1:A3";
9. Insert code
conditionalFormattingValueObject3.Append(formula3);
iconSet.Append(conditionalFormattingValueObject1); iconSet.Append(conditionalFormattingValueObject2); iconSet.Append(conditionalFormattingValueObject3); conditionalFormattingRule.Append(iconSet); conditionalFormatting.Append(conditionalFormattingRule); conditionalFormatting.Append(referenceSequence); if (!corruputFile) { conditionalFormattings.Append(conditionalFormatting); } worksheetExtension.Append(conditionalFormattings); worksheetExtensionList.Append(worksheetExtension); worksheetPart.Worksheet.Elements<PageMargins>().First().InsertAfterSelf(worksheetExtensionList);
10. Overwrite static void Main method with below:static void Main(string[] args){AddExtensionListToSpreadsheet ("ExtensionList.xlsx", "ExtensionList-Created.xlsx", false);}
Page 29
11. Press F5 to run
12. Navigate to <source directory>\bin\debug folder
13. Rename ExtensionList-Created.pptx to ExtensionList-Created.zip
14. Double click ExtensionList-Created.zip and navigate to and open \xl\worksheets\sheet1.xml
15. Find Extension List Section
Figure 33 - Ex 3 Task 2 Output
Note: Here we can see the newly created extension list we inserted into the document. All of the child elements are declared in the x14 namespace with one exception, the reference square element sqref which is declared in the xne namespace, we can see at the top of the page the x14 namespace is the spreadsheet 2009 namespace xmlns:x14="http://schemas.microsoft.com/office/spreadsheetml/2009/9/main" any markup consumer who doesn’t understand the x14 namespace will ignore these elements. and the xne is the spreadsheet 2006 namespace xmlns:xne="http://schemas.microsoft.com/office/excel/2006/main"
16. Open spreadsheet in Office 2007 and Office 2010 to see extension list in action
Page 30
Exercise 4 – Open XML Validation using the Open XML SDK (10 Minutes)In this exercise you will use the validation capability of the Open XML SDK to test the documents generated in Exercise 3. You will then deliberately introduce errors into those documents and use the error information returned in the ValidationErrorInfo class to identify and remedy the error.
You will be fixing schema level errors but the Open XML validator also validates against semantic errors, please see the bottom of this exercise for further discussion on this topic.
Task 1 – Create a simple console application to validate documents
In this task, you will:a. Create a simple console application to validate documentsb. Test the validation by introducing errors into the documents.
1. Locate ValidateOpenXml Method in MCEWalkthrough.cs
2. Underneath the //Todo comment declare a new instance of OpenXmlValidator class called validator passing in FileFormatVersions.Office2010 into the constructor
OpenXmlValidator validator = new OpenXmlValidator(FileFormatVersions.Office2010);
3. Insert the following codeIEnumerable<ValidationErrorInfo> openXmlErrors = validator.Validate(package);
Note: We are calling the Validate method on the validator object this takes either an OpenXmlElement, OpenXmlPart or an OpenXmlPackage.
4. The validate method returns a collection of ValidationErrorInfo objects if an error is encountered when validating the xml. We want to iterate through this list displaying all error messages.
Below line entered in step 3 create a foreach loop
foreach (ValidationErrorInfo error in openXmlErrors){
}
5. Paste the code below inside the foreach loop
Page 31
count++;Console.WriteLine("Error " + count);Console.WriteLine("Description: " + error.Description);Console.WriteLine("Path: " + error.Path.XPath);Console.WriteLine("Part: " + error.Part.Uri);Console.WriteLine("-------------------------------------------");
6. Overwrite static void Main method with code belowstatic void Main(string[] args){ValidateDocument();}
7. End
Task 2 – Test validation by introducing schema based and semantic errors
1. We have now created a simple console application for validating OpenXML packages, locate the ValidateDocument method.
We can see before this calls our newly created ValidateOpenXml method it calls AddExtensionListToSpreadsheet and ADDACBToPresentationML methods; but this time it passes in true as its final parameter which should hopefully corrupt the file, let’s run the application and see what happens.
Figure 34 - validate document method
Page 32
2. Press F5.
The console application first validates the spreadsheet file we created in Exercise 3 task 2 and displays one error
Figure 35 - Error output validating spreadsheet document
We can see be inspecting the error it tells us what the error is, the path and what xml part the error is located in.
Compare that to the error generated when opening the document in Office 2010, we can start to see the real value in the OpenXmlValidator
Figure 36 - Error message from Excel 2010
Page 33
3. Locate AddExtensionListToSpreadsheet method and find the line.
When ValidateDocument() called AddExtensionListToSpreadsheet method it passed in true to corrupt the file, this then skipped adding the Conditional Formatting tag to the extension list, this is exactly what the error told us.Description: The element has incomplete content. List of possible elements expected<….conditionalFormatting>
If we had forgotten this tag we would know straightaway if we were using the OpenXmlValidator and how to rectify the problem.
4. Bring the focus back to console application screen and press enter.
5. The console application now validates the PowerPoint document created earlier, this also generates an error.
Figure 37 - Error output validating against Office 2010 file format
6. The error message is telling us an AlternateContent element must contain a choice element. Inspecting the ADDACBToPresentationML method we can see the choice element is the exact element that has been excluded
Page 34
.
7. Press Enter8. We just validated the OpenXmlPackage against Office2010 file format lets change this to
Office2007 and observe any differences.
Edit line OpenXmlValidator validator = new OpenXmlValidator(FileFormatVersions.Office2010) to validate Office2007 file format
OpenXmlValidator validator = new OpenXmlValidator(FileFormatVersions.Office2007);
9. Press F510. Press Enter
Figure 38 - Error output validating against Office 2007 file format
The console application has validated both the Spreadsheet and PowerPoint presentation against the Office 2007 file format but this time it has returned no errors for the spreadsheet document, why?
If we look at the AddExtensionListToSpreadsheet method the tag conditionalFormatting we removed is declared within the x14 namespace which is a namespace Office2007 doesn’t understand
Page 35
so ignores all these elements when parsing them, Office2010 understands this namespace and tries to parse the Conditional Formatting section which is malformed and generates an error accordingly.
The PowerPoint document still produces an error when targeting either version of Office as we have removed a choice from an Alternate Content Block which is an element both version of Office will try and parse and are able to validate against the schema.
The two errors we have introduced are schema level errors. Errors where the xml structure doesn’t comply with the relevant xml schema, the OpenXmlValidator goes beyond Schema validation and also offers Semantic validation, this is validation not able to be expressed in an XSD language an example of semantic validation is enforcing the ISO footnote standard that a footnote contain a unique ID if the ID isn’t unique the document is said to be malformed, the OpenXmlValidator will capture these types of errors as well.
Technical Referencehttp://msdn.microsoft.com/en-us/library/bb497334.aspx
Page 36