Package.CreateRelationship 方法
定义
创建与给定的部件的包级别关系。Creates a package-level relationship to a given part.
重载
| CreateRelationship(Uri, TargetMode, String) |
使用给定的 URI、目标模式和关系类型创建与部件的包级别关系。Creates a package-level relationship to a part with a given URI, target mode, and relationship type. |
| CreateRelationship(Uri, TargetMode, String, String) |
使用给定的 URI、目标模式、关系类型和标识符 (ID) 创建与部件的包级别关系。Creates a package-level relationship to a part with a given URI, target mode, relationship type, and identifier (ID). |
示例
下面的示例说明如何在 PackageRelationship Package 和根文档部分之间创建。The following example illustrates creating 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);
' Add the Document part to the Package
Dim packagePartDocument As PackagePart = package.CreatePart(partUriDocument, System.Net.Mime.MediaTypeNames.Text.Xml)
' Copy the data to the Document Part
Using fileStream As New FileStream(documentPath, FileMode.Open, FileAccess.Read)
CopyStream(fileStream, packagePartDocument.GetStream())
End Using ' end:using(fileStream) - Close and dispose fileStream.
' Add a Package Relationship to the Document Part
package.CreateRelationship(packagePartDocument.Uri, TargetMode.Internal, PackageRelationshipType)
注解
包级别关系定义包和关联的目标部分或资源之间的关联。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.
在 Package 到包内的目标之间 PackagePart 。Between a Package to a target PackagePart inside the package.
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. 创建或删除关系的过程不会以任何方式在物理上改变目标部分或资源。The process of creating or deleting the relationship does not physically alter the target part or resource in any way.
有关其他信息,请参阅开放打包约定 (OPC) 规范可供下载 https://go.microsoft.com/fwlink/?LinkID=71255 。For additional information, see the Open Packaging Conventions (OPC) specification available for download at https://go.microsoft.com/fwlink/?LinkID=71255.
CreateRelationship(Uri, TargetMode, String)
使用给定的 URI、目标模式和关系类型创建与部件的包级别关系。Creates a package-level relationship to a part with a given URI, target mode, and relationship type.
public:
System::IO::Packaging::PackageRelationship ^ CreateRelationship(Uri ^ targetUri, System::IO::Packaging::TargetMode targetMode, System::String ^ relationshipType);
public System.IO.Packaging.PackageRelationship CreateRelationship (Uri targetUri, System.IO.Packaging.TargetMode targetMode, string relationshipType);
member this.CreateRelationship : Uri * System.IO.Packaging.TargetMode * string -> System.IO.Packaging.PackageRelationship
Public Function CreateRelationship (targetUri As Uri, targetMode As TargetMode, relationshipType As String) As PackageRelationship
参数
- targetUri
- Uri
目标部件的统一资源标识符 (URI)。The uniform resource identifier (URI) of the target part.
- targetMode
- TargetMode
指示目标部件对包来说是 Internal 还是 External。Indicates if the target part is Internal or External to the package.
- relationshipType
- String
唯一定义关系的角色的 URI。A URI that uniquely defines the role of the relationship.
返回
与指定部件的包级别关系。The package-level relationship to the specified part.
例外
targetUri 或 relationshipType 为 null。targetUri or relationshipType is null.
targetUri 部件为 PackageRelationship,或 targetMode 为 Internal,而 targetUri 为绝对 URI。The targetUri part is a PackageRelationship, or targetMode is Internal and targetUri is an absolute URI.
targetMode 的值无效。The value for targetMode is not valid.
包未打开(已调用了 Dispose(Boolean) 或 Close())。The package is not open (Dispose(Boolean) or Close() has been called).
包为只读。The package is read-only.
示例
下面的示例演示如何使用在 CreateRelationship PackageRelationship Package 和根文档部分之间创建。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);
' Add the Document part to the Package
Dim packagePartDocument As PackagePart = package.CreatePart(partUriDocument, System.Net.Mime.MediaTypeNames.Text.Xml)
' Copy the data to the Document Part
Using fileStream As New FileStream(documentPath, FileMode.Open, FileAccess.Read)
CopyStream(fileStream, packagePartDocument.GetStream())
End Using ' end:using(fileStream) - Close and dispose fileStream.
' Add a Package Relationship to the Document Part
package.CreateRelationship(packagePartDocument.Uri, TargetMode.Internal, PackageRelationshipType)
注解
包级别关系定义包和关联的目标部分或资源之间的关联。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.
在 Package 到包中的目标部分之间。Between a Package to a target part in the package.
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 不会以任何方式以物理方式更改目标部分或资源。CreateRelationship does not physically change the target part or resource in any way.
关系的目标不能是另一个关系。The target of a relationship cannot be another relationship.
如果 targetMode 指定为 Internal ,则 targetUri 必须是根据 RFC 3986 统一资源标识符构成的相对 uri) 泛型语法规范 (URI。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. 内部相对 URI 可以是以正斜杠开头的绝对路径 ( "/" ) 字符,如 "/page1.xaml" 或 "/images/picture4.jpg")或相对路径(如 ".。。/imagespicture1.jpg ",它解析为包根 ("/") 为基 URI。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.
如果 targetMode 将指定为 External ,则 targetUri 可以是根据 RFC 3986 统一资源标识符形成的绝对或相对 uri) 泛型语法规范 (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 引用外部目标资源 "page2.xml" 的绝对 URI 的示例。http://www.microsoft.com/page2.xml is an example of an absolute URI that references an external target resource "page2.xml". "images/picture1.jpg" 是相对 URI 的一个示例,它也引用外部目标资源 "1.jpg",但会根据包本身的 URI 进行解析。"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 必须是根据 RFC 3986 统一资源标识符(Uri) 泛型语法规范 (uri 组成的 uri。relationshipType must be a URI that is formed according to the RFC 3986 Uniform Resource Identifier (URI) Generic Syntax specification. 下表显示了 relationshipType 由开放打包约定 (OPC) 规范定义的包级别 uri。The following table shows the package-level relationshipType URIs defined by the Open Packaging Conventions (OPC) specification.
| 包级别关系Package-level Relationship | 关系类型 URIRelationship Type URI |
|---|---|
| 核心属性Core Properties | http://schemas.openxmlformats.org/package/2006/relationships/metadata/core-properties |
| 数字签名Digital Signature | http://schemas.openxmlformats.org/package/2006/relationships/digital-signature/signature |
| 数字签名证书Digital Signature Certificate | http://schemas.openxmlformats.org/package/2006/relationships/digital-signature/certificate |
| 数字签名源Digital Signature Origin | http://schemas.openxmlformats.org/package/2006/relationships/digital-signature/origin |
| 缩略图Thumbnail | http://schemas.openxmlformats.org/package/2006/relationships/metadata/thumbnail |
有关打包和包关系的详细信息,请参阅 (OPC) 规范的开放打包约定第1.3 节,可在下载 https://go.microsoft.com/fwlink/?LinkID=71255 。For more information about packaging and package relationships, see section 1.3 of the Open Packaging Conventions (OPC) specification available for download at https://go.microsoft.com/fwlink/?LinkID=71255.
另请参阅
- DeleteRelationship(String)
- GetRelationship(String)
- GetRelationships()
- GetRelationshipsByType(String)
- RelationshipExists(String)
适用于
CreateRelationship(Uri, TargetMode, String, String)
使用给定的 URI、目标模式、关系类型和标识符 (ID) 创建与部件的包级别关系。Creates a package-level relationship to a part with a given URI, target mode, relationship type, and identifier (ID).
public:
System::IO::Packaging::PackageRelationship ^ CreateRelationship(Uri ^ targetUri, System::IO::Packaging::TargetMode targetMode, System::String ^ relationshipType, System::String ^ id);
public System.IO.Packaging.PackageRelationship CreateRelationship (Uri targetUri, System.IO.Packaging.TargetMode targetMode, string relationshipType, string id);
public System.IO.Packaging.PackageRelationship CreateRelationship (Uri targetUri, System.IO.Packaging.TargetMode targetMode, string relationshipType, string? id);
member this.CreateRelationship : Uri * System.IO.Packaging.TargetMode * string * string -> System.IO.Packaging.PackageRelationship
Public Function CreateRelationship (targetUri As Uri, targetMode As TargetMode, relationshipType As String, id As String) As PackageRelationship
参数
- targetUri
- Uri
目标部件的统一资源标识符 (URI)。The uniform resource identifier (URI) of the target part.
- targetMode
- TargetMode
指示目标部件对包来说是 Internal 还是 External。Indicates if the target part is Internal or External to the package.
- relationshipType
- String
唯一定义关系的角色的 URI。A URI that uniquely defines the role of the relationship.
- id
- String
唯一 XML 标识符。A unique XML identifier.
返回
与指定部件的包级别关系。The package-level relationship to the specified part.
例外
targetUri 或 relationshipType 为 null。targetUri or relationshipType is null.
targetUri 部件为 PackageRelationship,或 targetMode 为 Internal,而 targetUri 为绝对 URI。The targetUri part is a PackageRelationship, or targetMode is Internal and targetUri is an absolute URI.
targetMode 的值无效。The value for targetMode is not valid.
包未打开(已调用了 Dispose(Boolean) 或 Close())。The package is not open (Dispose(Boolean) or Close() has been called).
包为只读。The package is read-only.
id 不是有效的 XML 标识符;或者包中已存在具有指定的 id 的部件。id is not a valid XML identifier; or a part with the specified id already occurs in the package.
示例
下面的示例演示如何使用在 CreateRelationship PackageRelationship Package 和根文档部分之间创建。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);
' Add the Document part to the Package
Dim packagePartDocument As PackagePart = package.CreatePart(partUriDocument, System.Net.Mime.MediaTypeNames.Text.Xml)
' Copy the data to the Document Part
Using fileStream As New FileStream(documentPath, FileMode.Open, FileAccess.Read)
CopyStream(fileStream, packagePartDocument.GetStream())
End Using ' end:using(fileStream) - Close and dispose fileStream.
' Add a Package Relationship to the Document Part
package.CreateRelationship(packagePartDocument.Uri, TargetMode.Internal, PackageRelationshipType)
注解
包级别关系定义包和关联的目标部分或资源之间的关联。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.
在 Package 到包中的目标部分之间。Between a Package to a target part in the package.
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 不会以任何方式以物理方式更改目标部分或资源。CreateRelationship does not physically change the target part or resource in any way.
关系的目标不能是另一个关系。The target of a relationship cannot be another relationship.
id 必须是有效的 XML 标识符。id must be a valid XML identifier. id类型为 xsd: ID,必须遵循 XML 架构第2部分:数据类型规范中指定的命名约定 (参阅 https://www.w3.org/TR/xmlschema-2/#ID) 。The id type is xsd:ID and must follow the naming conventions prescribed in the XML Schema Part 2: Datatypes specification (see https://www.w3.org/TR/xmlschema-2/#ID).
如果 id 指定为,则 null 将自动生成唯一 ID。If id is specified as null a unique ID will be automatically generated. id由空字符串指定的无效。An id specified by an empty string is not valid.
如果 targetMode 指定为 Internal ,则 targetUri 必须是根据 RFC 3986 统一资源标识符构成的相对 uri) 泛型语法规范 (URI。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. 内部相对 URI 可以是以正斜杠开头的绝对路径 ( "/" ) 字符,如 "/page1.xaml" 或 "/images/picture4.jpg")或相对路径(如 ".。。/imagespicture1.jpg ",它解析为包根 ("/") 为基 URI。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.
如果 targetMode 将指定为 External ,则 targetUri 可以是根据 RFC 3986 统一资源标识符形成的绝对或相对 uri) 泛型语法规范 (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 引用外部目标资源 "page2.xml" 的绝对 URI 的示例。http://www.microsoft.com/page2.xml is an example of an absolute URI that references an external target resource "page2.xml". "images/picture1.jpg" 是相对 URI 的一个示例,它也引用外部目标资源 "1.jpg",但会根据包本身的 URI 进行解析。"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 必须是根据 RFC 3986 统一资源标识符(Uri) 泛型语法规范 (uri 组成的 uri。relationshipType must be a URI that is formed according to the RFC 3986 Uniform Resource Identifier (URI) Generic Syntax specification. 下表显示了 relationshipType 由开放打包约定 (OPC) 规范定义的包级别 uri。The following table shows the package-level relationshipType URIs defined by the Open Packaging Conventions (OPC) specification.
| 包级别关系Package-level Relationship | 关系类型 URIRelationship Type URI |
|---|---|
| 核心属性Core Properties | http://schemas.openxmlformats.org/package/2006/relationships/metadata/core-properties |
| 数字签名Digital Signature | http://schemas.openxmlformats.org/package/2006/relationships/digital-signature/signature |
| 数字签名证书Digital Signature Certificate | http://schemas.openxmlformats.org/package/2006/relationships/digital-signature/certificate |
| 数字签名源Digital Signature Origin | http://schemas.openxmlformats.org/package/2006/relationships/digital-signature/origin |
| 缩略图Thumbnail | http://schemas.openxmlformats.org/package/2006/relationships/metadata/thumbnail |
有关打包和包关系的其他信息,请参阅 (OPC) 规范的开放打包约定第1.3 节,可在下载 https://go.microsoft.com/fwlink/?LinkID=71255 。For additional information about packaging and package relationships, see section 1.3 of the Open Packaging Conventions (OPC) specification available for download at https://go.microsoft.com/fwlink/?LinkID=71255.
另请参阅
- CreateRelationship(Uri, TargetMode, String)
- DeleteRelationship(String)
- GetRelationship(String)
- GetRelationships()
- GetRelationshipsByType(String)
- RelationshipExists(String)