Export (0) Print
Expand All
0 out of 1 rated this helpful - Rate this topic

Package.CreateRelationship Method (Uri, TargetMode, String)

Creates a package-level relationship to a part with a given URI, target mode, and relationship type.

Namespace:  System.IO.Packaging
Assembly:  WindowsBase (in WindowsBase.dll)
public PackageRelationship CreateRelationship(
	Uri targetUri,
	TargetMode targetMode,
	string relationshipType
)

Parameters

targetUri
Type: System.Uri

The uniform resource identifier (URI) of the target part.

targetMode
Type: System.IO.Packaging.TargetMode

Indicates if the target part is Internal or External to the package.

relationshipType
Type: System.String

A URI that uniquely defines the role of the relationship.

Return Value

Type: System.IO.Packaging.PackageRelationship
The package-level relationship to the specified part.
ExceptionCondition
ArgumentNullException

targetUri or relationshipType is null.

ArgumentException

The targetUri part is a PackageRelationship, or targetMode is Internal and targetUri is an absolute URI.

ArgumentOutOfRangeException

The value for targetMode is not valid.

ObjectDisposedException

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

IOException

The package is read-only.

A package-level relationship defines an association between the package and an associated target part or resource. A package-level relationship can be one of two forms.

  • Between a Package to a target part in the package.

  • Between a Package to a target resource outside the package.

In a package-relationship the package is considered the "owner" of the relationship. When the package is deleted, all the relationships owned by the package are also deleted.

CreateRelationship does not physically change the target part or resource in any way.

The target of a relationship cannot be another relationship.

If targetMode is specified as Internal, targetUri must be a relative URI that is formed according to the RFC 3986 Uniform Resource Identifier (URI) Generic Syntax specification. The internal relative URI can be either an absolute path that starts with a forward slash ("/") character such as "/page1.xaml" or "/images/picture4.jpg", or a relative path such as "../imagespicture1.jpg" that resolves against the package root ("/") as the base URI.

If targetMode is specified as External, targetUri can be either an absolute or relative URI formed according to the RFC 3986 Uniform Resource Identifier (URI) Generic Syntax specification. "http://www.microsoft.com/page2.xml" is an example of an absolute URI that references an external target resource "page2.xml". "images/picture1.jpg" is an example of a relative URI that also references an external target resource "1.jpg" but which resolves against the URI of the package itself.

relationshipType must be a URI that is formed according to the RFC 3986 Uniform Resource Identifier (URI) Generic Syntax specification. 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 more information about packaging and package relationships, see section 1.3 of the Open Packaging Conventions (OPC) specification available for download at http://go.microsoft.com/fwlink/?LinkID=71255.

The following example illustrates how to use CreateRelationship to create a PackageRelationship between a Package and a root document part. For the complete sample, see Writing a Package Sample.

// Add the Document part to the Package
PackagePart packagePartDocument =
    package.CreatePart(partUriDocument,
                   System.Net.Mime.MediaTypeNames.Text.Xml);

// Copy the data to the Document Part 
using (FileStream fileStream = new FileStream(
       documentPath, FileMode.Open, FileAccess.Read))
{
    CopyStream(fileStream, packagePartDocument.GetStream());
}// end:using(fileStream) - Close and dispose fileStream. 

// Add a Package Relationship to the Document Part
package.CreateRelationship(packagePartDocument.Uri,
                           TargetMode.Internal,
                           PackageRelationshipType);

.NET Framework

Supported in: 4.5.1, 4.5, 4, 3.5, 3.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, 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.

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.