Package.GetRelationshipsByType Method
Returns a collection of all the package-level relationships that match a given RelationshipType.
Assembly: WindowsBase (in WindowsBase.dll)
Parameters
- relationshipType
- Type: System.String
The RelationshipType to match and return in the collection.
Return Value
Type: System.IO.Packaging.PackageRelationshipCollectionA collection of package-level relationships that match the specified relationshipType.
| Exception | Condition |
|---|---|
| ArgumentNullException | relationshipType is null. |
| ArgumentException | relationshipType is an empty string. |
| ObjectDisposedException | |
| IOException | The package is write-only. |
GetRelationships will never return null; however the returned collection may contain zero elements if there are no package-level relationships that match the specified relationshipType.
The following table shows the package-level relationshipType URIs defined by the Open Packaging Conventions (OPC) specification.
Package-level Relationship | Relationship Type URI |
|---|---|
Core Properties | http://schemas.openxmlformats.org/package/2006/relationships/meta data/core-properties |
Digital Signature | http://schemas.openxmlformats.org/package/2006/relationships/digit al-signature/signature |
Digital Signature Certificate | http://schemas.openxmlformats.org/package/2006/relationships/digit al-signature/certificate |
Digital Signature Origin | http://schemas.openxmlformats.org/package/2006/relationships/digit al-signature/origin |
Thumbnail | http://schemas.openxmlformats.org/package/2006/relationships/meta data/thumbnail |
For additional information, see the Open Packaging Conventions (OPC) specification available for download at http://go.microsoft.com/fwlink/?LinkID=71255.
The following example shows how to retrieve the relationships that have been defined for the package. For the complete sample, see Reading a Package Sample.
// Open the Package. // ('using' statement insures that 'package' is // closed and disposed when it goes out of scope.) using (Package package = Package.Open(packagePath, FileMode.Open, FileAccess.Read)) { PackagePart documentPart = null; PackagePart resourcePart = null; // Get the Package Relationships and look for // the Document part based on the RelationshipType Uri uriDocumentTarget = null; foreach (PackageRelationship relationship in package.GetRelationshipsByType(PackageRelationshipType)) { // Resolve the Relationship Target Uri // so the Document Part can be retrieved. uriDocumentTarget = PackUriHelper.ResolvePartUri( new Uri("/", UriKind.Relative), relationship.TargetUri); // Open the Document Part, write the contents to a file. documentPart = package.GetPart(uriDocumentTarget); ExtractPart(documentPart, targetDirectory); } // Get the Document part's Relationships, // and look for required resources. Uri uriResourceTarget = null; foreach (PackageRelationship relationship in documentPart.GetRelationshipsByType( ResourceRelationshipType)) { // Resolve the Relationship Target Uri // so the Resource Part can be retrieved. uriResourceTarget = PackUriHelper.ResolvePartUri( documentPart.Uri, relationship.TargetUri); // Open the Resource Part and write the contents to a file. resourcePart = package.GetPart(uriResourceTarget); ExtractPart(resourcePart, targetDirectory); } }// end:using(Package package) - Close & dispose package.
Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.