PackUriHelper.Create Method (Uri, Uri, String)


Creates a pack URI given a Package URI, the URI of a part in the package, and a "#" fragment to append.

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

public static Uri Create(
	Uri packageUri,
	Uri partUri,
	string fragment


Type: System.Uri

The URI of the Package.

Type: System.Uri

The URI of the PackagePart in the package.

Type: System.String

A "#" reference identifying an element within the package part.

Return Value

Type: System.Uri

The pack URI that identifies the specified package, package part, and fragment.

Exception Condition

packageUri is null.


packageUri is not an absolute URI.


partUri is not a valid part URI syntax.


fragment is empty or does begin with "#".

packageUri may not be specified as null or empty.

If partUri is null, the returned pack URI points to the package.

fragment cannot be an empty string but can be specified as null. When not specified as null, the fragment string must begin with a '#' character. For more information of the syntax of fragment references, see Section 3.5 "Fragment" of RFC 3986.

The following table illustrates sample cases for the Create method.




 Returned pack URI 
































Composing a pack URI is a multi-step process. For example, one step in forming a pack URI is to replace the forward slash (/) characters of the packageUri with commas (,).

For more information on string conversion and how pack URIs are formed, see Appendix A.4 "String Conversion Examples" and Appendix B.3 "Composing a Pack URI" in the Open Packaging Conventions specification available for download at Specifications and License Downloads.

The following example shows how to use the Create(Uri) method to define a pack URI that references a package.

// ------------------------ GetFixedDocument --------------------------
/// <summary>
///   Returns the fixed document at a given URI within
///   the currently open XPS package.</summary>
/// <param name="fixedDocUri">
///   The URI of the fixed document to return.</param>
/// <returns>
///   The fixed document at a given URI
///   within the current XPS package.</returns>
private FixedDocument GetFixedDocument(Uri fixedDocUri)
    FixedDocument fixedDocument = null;

    // Create the URI for the fixed document within the package. The URI
    // is used to set the Parser context so fonts & other items can load.
    Uri tempUri = new Uri(_xpsDocumentPath, UriKind.RelativeOrAbsolute);
    ParserContext parserContext = new ParserContext();
    parserContext.BaseUri = PackUriHelper.Create(tempUri);

    // Retreive the fixed document.
    PackagePart fixedDocPart = _xpsPackage.GetPart(fixedDocUri);
    if (fixedDocPart != null)
        object fixedObject =
            XamlReader.Load(fixedDocPart.GetStream(), parserContext);
        if (fixedObject != null)
            fixedDocument = fixedObject as FixedDocument;

    return fixedDocument;
}// end:GetFixedDocument()

.NET Framework
Available since 3.0
Return to top