Package.GetRelationshipsByType(String) 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
回傳所有與給定 RelationshipType相符的套件層級關係集合。
public:
System::IO::Packaging::PackageRelationshipCollection ^ GetRelationshipsByType(System::String ^ relationshipType);
public System.IO.Packaging.PackageRelationshipCollection GetRelationshipsByType(string relationshipType);
member this.GetRelationshipsByType : string -> System.IO.Packaging.PackageRelationshipCollection
Public Function GetRelationshipsByType (relationshipType As String) As PackageRelationshipCollection
參數
- relationshipType
- String
該系列 RelationshipType 中會匹配並回歸。
傳回
一組符合指定 relationshipType的套件層級關係集合。
例外狀況
relationshipType 是 null。
relationshipType 是空字串。
包裹還沒開(Dispose(Boolean) 也 Close() 沒被叫過)。
該套件僅為寫入。
範例
以下範例說明如何取得已定義的套件關聯。
// 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.
' Open the Package.
' ('using' statement insures that 'package' is
' closed and disposed when it goes out of scope.)
Using package As Package = Package.Open(packagePath, FileMode.Open, FileAccess.Read)
Dim documentPart As PackagePart = Nothing
Dim resourcePart As PackagePart = Nothing
' Get the Package Relationships and look for
' the Document part based on the RelationshipType
Dim uriDocumentTarget As Uri = Nothing
For Each relationship As PackageRelationship 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)
Next relationship
' Get the Document part's Relationships,
' and look for required resources.
Dim uriResourceTarget As Uri = Nothing
For Each relationship As PackageRelationship 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)
Next relationship
End Using ' end:using(Package package) - Close & dispose package.
備註
GetRelationships 永遠不會回傳 null;然而,若沒有與指定 relationshipType的套件層級關係相符,回傳的集合可能包含零元素。
下表顯示了由開放封裝慣例(OPC)規範所定義的套件層級 relationshipType URI。
| 套件層級關係 | 關係類型 URI |
|---|---|
| 核心特性 | http://schemas.openxmlformats.org/package/2006/relationships/metadata/core-properties |
| 數位簽章 | http://schemas.openxmlformats.org/package/2006/relationships/digital-signature/signature |
| 數位簽章憑證 | http://schemas.openxmlformats.org/package/2006/relationships/digital-signature/certificate |
| 數位簽章起源 | http://schemas.openxmlformats.org/package/2006/relationships/digital-signature/origin |
| 縮圖 | http://schemas.openxmlformats.org/package/2006/relationships/metadata/thumbnail |
欲了解更多資訊,請參閱可於 .(可下載) https://www.ecma-international.org/publications-and-standards/standards/ecma-376/的開放包裝公約(OPC)規範。