Package.GetRelationshipsByType Method

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
)
You cannot use methods in XAML.

Parameters

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.

ExceptionCondition
ArgumentNullException

relationshipType is null.

ArgumentException

relationshipType is an empty string.

ObjectDisposedException

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

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 7, Windows Vista, Windows XP SP2, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0
Was this page helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft