GetRelationshipsByType Method

Package.GetRelationshipsByType Method (String)


Returns a collection of all the package-level relationships that match a given RelationshipType.

Namespace:   System.IO.Packaging
Assembly:  WindowsBase (in WindowsBase.dll)

public PackageRelationshipCollection GetRelationshipsByType(
	string relationshipType


Type: System.String

The RelationshipType to match and return in the collection.

Return Value

Type: System.IO.Packaging.PackageRelationshipCollection

A collection of package-level relationships that match the specified relationshipType.

Exception Condition

relationshipType is null.


relationshipType is an empty string.


The package is not open (Dispose or Close has been called).


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 data/core-properties

Digital Signature al-signature/signature

Digital Signature Certificate al-signature/certificate

Digital Signature Origin al-signature/origin

Thumbnail data/thumbnail

For additional information, see the Open Packaging Conventions (OPC) specification available for download at

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
        // 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
        // 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.

.NET Framework
Available since 3.0
Return to top
© 2015 Microsoft