Presentation is loading. Please wait.

Presentation is loading. Please wait.

Open XML Developer Workshop Open XML Packages, Parts and Relationships.

Similar presentations


Presentation on theme: "Open XML Developer Workshop Open XML Packages, Parts and Relationships."— Presentation transcript:

1 Open XML Developer Workshop Open XML Packages, Parts and Relationships

2 Open XML Developer Workshop Disclaimer The information contained in this slide deck represents the current view of Microsoft Corporation on the issues discussed as of the date of publication. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information presented after the date of publication. This slide deck is for informational purposes only. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS DOCUMENT. Complying with all applicable copyright laws is the responsibility of the user. Without limiting the rights under copyright, no part of this slide deck 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 slide deck. Except as expressly provided in any written license agreement from Microsoft, the furnishing of this slide deck 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. © 2006 Microsoft Corporation. All rights reserved. Microsoft, 2007 Microsoft Office System,.NET Framework 3.0, Visual Studio, and Windows Vista 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.

3 Open XML Developer Workshop Overview Introduction to the Open Packaging Convention Working with System.IO.Packaging

4 Open XML Developer Workshop Lesson: Introduction to the Open Packaging Convention Open XML Formats Architecture Components of the Open Packaging Convention

5 Open XML Developer Workshop Elements of the Open Packaging Convention Package – The container Document Parts – The components of the document Content Types Stream – defines content type of parts Relationships – references to other parts of the document “Start Part” – The root of the document Common Package Parts Package Relationships Core Properties Digital Signatures Specific Format Parts Office Document Part Relationships Binary Part XML Part Part Rels Etc… Content Types Stream

6 Open XML Developer Workshop The Package A package is A logical entity that holds a collection of parts An aggregation of all pieces of a document in a single object Not tied to a physical representation ZIP, Database, loose files Special features Core properties Thumbnail parts Digital signatures

7 Open XML Developer Workshop Document Parts A part is analogous to a file on the file system stored inside the package in a specific location reachable via a URI stored with a specific content type mainly XML but other native types as well Images, sounds, video, OLE objects Content type is enforced Example: cannot tag JPEG part as GIF A part can use growth hints to allow uninterrupted content growth

8 Open XML Developer Workshop Content Types Define the type of media stored in the part. The content types stream Defines mappings from part to content type Is not directly addressable Default values and part specific overrides <Default Extension="rels" ContentType= "application/vnd.openxmlformats-package.relationships+xml" /> <Override PartName="/word/document.xml" ContentType= "application/vnd.openxmlformats- officedocument.wordprocessingml.document.main+xml"/> [Content_Types].xml Defaults Overrides

9 Open XML Developer Workshop Relationships Relationships are stored in XML streams in the package Ties elements inside the package to each other Allows navigation of document without parsing parts Package relationships stream URI: /_rels/.rels Part relationships stream URI: _rels/[partname].rels <Relationship Id=“rId1“ Type="http://schemas.openxmlformats.org/officeDocument /2006/relationships/officeDocument" Target="document.xml"/> Relationship Type Target URI Relationship ID

10 Open XML Developer Workshop Relationships A relationship has its own type The type gives meaning to the content in a part for a given context A way to say “The styles part is stored at that location” /document.xml …wordprocessingml.document.main+xml /styles.xml …wordprocessingml.styles+xml http://schemas.openxmlformats.org/officeDocument/2006/relationships/styles /styles.xml Part URI Part content type Part URI Part content type Rel. type _rels/document.xml.rels Target URI

11 Open XML Developer Workshop Inside an Open XML Package

12 Open XML Developer Workshop Identifying the OfficeDocument “start part” Your Code Content Type Stream [Content_Types].xml Package Relationships /_rels/.rels Office Document Part e.g. /document.xml GetRelationship (Rel.Type) : “http://schemas.../officeDocument” PartUri : “/document.xml” The OPC Package ContentType : “application/vnd…wordprocessingml.document.main+xml” GetContentType (PartUri) : “/documents.xml” GetPart (PartUri) : “/document.xml” Part: XML Default extension or URI override

13 Open XML Developer Workshop Identifying implicitly related parts Your Code Doc.Part document.xml Part Relationships /_rels/document.xml.rels Related Part /styles.xml GetRelationship (Rel.Type): “http://schemas.../styles” PartUri : “/styles.xml” GetPart (PartUri): “/styles.xml” Part: XML ContentType: “application/vnd.openxmlformats-officedocument.wordprocessingml.styles+xml” GetContentType (PartUri): “/styles.xml” The OPC Package Content Type Stream [Content_Types].xml GetRelationship (Rel.Type): “http://schemas.../styles” PartUri : “/styles.xml” Default extension or URI override

14 Open XML Developer Workshop Identifying explicitly related parts Your Code Doc.Part document.xml Part Relationships /_rels/document.xml.rels Related Part /pict.png GetRelationship (Rel.ID): “rId1” PartUri : “/pict.png” GetPart (PartUri): “/pict.png” Part: Binary ContentType (Default extension or Override Uri): “image/png” GetPartType (PartUri): “/pict.png” The OPC Package Content Type Stream [Content_Types].xml GetRelationship (Rel.ID): “rId1” PartUri : “/pict.png” Default extension or URI override

15 Open XML Developer Workshop Open source tool written by Wouter van Vugt http://www.codeplex.com/ Package Explorer

16 Open XML Developer Workshop Lesson: Working with System.IO.Packaging Components of System.IO.Packaging Traversing a Package Manipulating a Package Open XML Password Hashing

17 Open XML Developer Workshop Components of System.IO.Packaging The library Part of Windows Presentation FoundationPart of Windows Presentation Foundation Installed with.NET 3.0Installed with.NET 3.0 Requires.NET 2.0 RuntimeRequires.NET 2.0 Runtime Enables package manipulation forEnables package manipulation for Open XML File FormatsOpen XML File Formats XML Paper Specification FilesXML Paper Specification Files Any Open Packaging Convention filesAny Open Packaging Convention files

18 Open XML Developer Workshop Components of System.IO.Packaging The Package Package class provides methods to create, enumerate and delete the following entities Package Package PropertiesPackage PropertiesPackageRelationshipsPackageParts Common Package Parts Package Relationships Core Properties Digital Signatures Specific Format Parts Office Document Part Relationships XML Part Part Rels Etc…

19 Open XML Developer Workshop Components of System.IO.Packaging The PackageRelationship Required to find parts (part names are not guaranteed) Iterate through a RelationshipCollection by type or ID Relationship PropertiesRelationship PropertiesIDPackageRelationshipTypeSourceUriTargetModeTargetUri Common Package Parts Package Relationships Core Properties Digital Signatures Specific Format Parts Office Document Part Relationships XML Part Part Rels Etc…

20 Open XML Developer Workshop Components of System.IO.Packaging The PackagePart A PackagePart is the object of data within the Package It provides support to create, enumerate and delete part relationships Get data as a System.IO.Stream PackagePart properties:PackagePart properties:CompressionOptionContentTypePackageUri Common Package Parts Package Relationships Core Properties Digital Signatures Specific Format Parts Office Document Part Relationships XML Part Part Rels Etc…

21 Open XML Developer Workshop Components of System.IO.Packaging The PackUriHelper Find a related PackagePart by searching relationships, either by relationship type or relationship ID This returns a list of PackageRelationship objectsThis returns a list of PackageRelationship objects A PackageRelationship defines two relative URIsA PackageRelationship defines two relative URIs Source URI, pointing to the source PackagePartSource URI, pointing to the source PackagePart Target URI, pointing to the target PackagePartTarget URI, pointing to the target PackagePart Retrieve a PackagePart by using a URI relative to the root of the Package Translation of Source and Target URIs is requiredTranslation of Source and Target URIs is required Use the PackUriHelper class to aid in the translationUse the PackUriHelper class to aid in the translation

22 Open XML Developer Workshop <Relationship Id="rId1" Type="http://schemas.openxmlformats.org/ officeDocument/2006/relationships/officeDocument" Target="word/document.xml" /> Traversing a Package Opening the “Start-Part”

23 Open XML Developer Workshop <Relationship Id="rId1" Type="http://schemas.openxmlformats.org/ officeDocument/2006/relationships/officeDocument" Target="word/document.xml" /> <w:document xmlns:w="http://schemas.openxmlformats.org/ wordprocessingml/2006/main"> Hello World! Traversing a Package Opening the “Start-Part”

24 Open XML Developer Workshop Traversing a Package Opening the “Start-Part” Now using the Packaging API string relationshipType = "http://schemas.openxmlformats.org/..."; using (Package package = Package.Open("mydocument.docx")) { PackageRelationship relationship = null; foreach (PackageRelationship searchRelation in package.GetRelationshipsByType(relationshipType)) { relationship = searchRelation; break; } Uri partUri = PackUriHelper.ResolvePartUri( relationship.SourceUri, relationship.TargetUri); PackagePart mainPart = package.GetPart(partUri); // Do something with mainPart }

25 Open XML Developer Workshop <Relationship Id="rId5" Type="http://schemas.openxmlformats.org/officeDocument/2006/ relationships/theme" Target="theme/theme1.xml" /> Traversing a Package Opening a related document part

26 Open XML Developer Workshop <Relationship Id="rId5" Type="http://schemas.openxmlformats.org/officeDocument/2006/ relationships/theme" Target="theme/theme1.xml" /> <a:theme xmlns:a="http://schemas.openxmlformats.org/drawi ngml/2006/main" name="Office Theme"> Traversing a Package Opening a related document part

27 Open XML Developer Workshop Traversing a Package Opening a related document part You can retrieve a Part using a relationshipID Or using the relationshipType string relID= “ rId1 ” ; PackageRelationship imagerelationship = mainPart.GetRelationship(relID); Uri imagePartUri = PackUriHelper.ResolvePartUri( imagerelationship.SourceUri, imagerelationship.TargetUri); PackagePart imagepart = mainPart.Package.GetPart(imagePartUri); string RT_Image = “http://schemas.openxmlformats.org/.../image ” ; int imageCount = 0; foreach (PackageRelationship relation in mainPart.GetRelationshipsByType(RT_Image)) { imageCount++; } Console.WriteLine("The part references {0} unique images", imageCount);

28 Open XML Developer Workshop Traversing a Package

29 Open XML Developer Workshop Creating a New Package The Packaging API allows you to create new Packages based on a file or stream Package.Open creates the Package when using the correct parameters using (Package package = Package.Open("newFile.xlsx", FileMode.OpenOrCreate, FileAccess.ReadWrite)) { // the Package is now on disk, 0 bytes in size }

30 Open XML Developer Workshop Creating a Part Create a PackagePart using Location (Uri) Content type (String) You can not alter the content type later on Make sure the location is not in use Use a Stream to modify the PackagePart contents string contentType = “application/vnd.openxmlformats- officedocument.spreadsheetml.sheet.main+xml”; PackagePart part = package.CreatePart( new Uri(“workbook.xml”, UriKind.Relative), contentType); using (StreamWriter writer = new StreamWriter( part.GetStream(FileMode.Create, FileAccess.Write))) { using (StreamReader reader = new StreamReader(path)) { writer.Write(reader.ReadToEnd()); }

31 Open XML Developer Workshop Creating Part Relationships Each PackagePart needs to be related to its child parts Relationship ID, RelationshipType, TargetMode using (Package package = Package.Open("newFile.xlsx", FileMode.OpenOrCreate, FileAccess.ReadWrite)) { PackagePart workbook = package.CreatePart( new Uri(“/workbook.xml”, UriKind.Relative), “application/vnd...sheet.main+xml”); PackagePart sheet = package.CreatePart( new Uri(“/sheet1.xml”, UriKind.Relative), “application/vnd...worksheet.main+xml”, package); package.CreateRelationship( new Uri("/workbook.xml", UriKind.Relative), TargetMode.Internal, "http://schemas.../relationships/officeDocument", "rId1"); workbook.CreateRelationship( new Uri("/sheet1.xml", UriKind.Relative), TargetMode.Internal, "http://schemas.../relationships/worksheet", "rId1"); } Start Part Related Part

32 Open XML Developer Workshop Digital Signatures Digital Signatures in the Package Allows the validation of package integrity using X.509 certificates You can sign All parts, including digital signatures All relationships, or a subset of a set of relationships Navigate signatures with the Digital Signature Origin Part

33 Open XML Developer Workshop Digital Signatures Digital Signature Origin Part Digital Signature XML Signature Part Signed Part Relationship URI X.509 Certificate Part X.509

34 Open XML Developer Workshop Microsoft SDK for Open XML Formats Microsoft.Office.DocumentFormat.OpenXml Library of strongly typed parts: Document start part, styles part, etc. Classes for Open XML elements Consistent high-level interface To the packaging API, “parts is parts” To the SDK for Open XML, each part has a type based on its content and markup structure

35 Open XML Developer Workshop SDK Code Sample // How to: Replace a part in a package. public static void ReplaceTheme(string document, string themeFile) { using (WordprocessingDocument wordDoc = WordprocessingDocument.Open(document, true)) { MainDocumentPart mainPart = wordDoc.MainDocumentPart; // Delete the old part. mainPart.DeletePart(mainPart.ThemePart); // Add a new part and then add content. ThemePart themePart = mainPart.AddNewPart ( ); using (StreamReader streamReader = new StreamReader(themeFile)) using (StreamWriter streamWriter = new StreamWriter( themePart.GetStream(FileMode.Create ) )) { streamWriter.Write(streamReader.ReadToEnd( )); } See LAB08 and LAB08SDK for a comparison of the SDK and System.IO.Packaging.

36 Open XML Developer Workshop Where to get the SDK for Open XML Download the June CTP: http://www.microsoft.com/downloads/details.aspx?FamilyId=AD0B72FB- 4A1D-4C52-BDB5-7DD7E816D046&displaylang=en http://www.microsoft.com/downloads/details.aspx?FamilyId=AD0B72FB- 4A1D-4C52-BDB5-7DD7E816D046&displaylang=en MSDN documentation: http://msdn2.microsoft.com/en-us/library/bb448854.aspx http://msdn2.microsoft.com/en-us/library/bb448854.aspx MSDN support forum: http://forums.microsoft.com/MSDN/ShowForum.aspx?ForumID=1647&Site ID=1 http://forums.microsoft.com/MSDN/ShowForum.aspx?ForumID=1647&Site ID=1

37 Open XML Developer Workshop Files and folders – NO!Parts and relationships – YES How to think about OPC packages

38 Open XML Developer Workshop Resources OpenXMLDeveloper.org Kevin Boske’s blog http://blogs.msdn.com/kevinboske Brian Jones’s blog http://blogs.msdn.com/brian_jones Doug Mahugh’s blog http://blogs.msdn.com/dmahugh XPS Blog: http://blogs.msdn.com/xps

39 Open XML Developer Workshop

40 Open XML Password Hashing Password: testCharacters: 4Initial Hash: 0 Hash from last to first character hash = ((hash >> 14) & 0x01) | ((hash << 1) & 0x7fff); hash ^= passwordCharacters[charIndex]; Hash using the character count hash = ((hash >> 14) & 0x01) | ((hash << 1) & 0x7fff); hash ^= characterCount; Hash use a special value hash ^= (0x8000 | ('N' << 8) | 'K'); Convert the hash value to a hexadecimal string Hashed value: CBEB


Download ppt "Open XML Developer Workshop Open XML Packages, Parts and Relationships."

Similar presentations


Ads by Google