dotnet/wpf · error · FileFormatException
SR.XpsValidatingLoaderUriNotInSamePackage
Error message
SR.XpsValidatingLoaderUriNotInSamePackage
What it means
XPSS0ValidatingLoader.Load throws FileFormatException (SR.XpsValidatingLoaderUriNotInSamePackage) when loading a part inside an XPS package whose parent URI belongs to a different package. XPS parts must resolve within the same package as their parent; cross-package references are rejected in strict document mode.
Solutions
- Fix the parent/base URI so it points into the same package as the part being loaded.
- Remove or correct relationships that cross package boundaries; XPS requires all references within one package.
- Regenerate the XPS package with a compliant producer so all URIs share one package URI.
- Verify URIs with PackUriHelper.GetPackageUri before loading to confirm they match.
Example fix
// before
pc.BaseUri = new Uri("pack://application:,,,/FixedPage.xaml"); // different package
// after
pc.BaseUri = new Uri("pack://container:,,,/Documents/1/Pages/1.fpage"); // same package as part Defensive patterns
Strategy: validation
Validate before calling
static bool SamePackage(Uri baseUri, Uri parentUri)
{
var a = System.IO.Packaging.PackUriHelper.GetPackageUri(baseUri);
var b = parentUri == null ? a : System.IO.Packaging.PackUriHelper.GetPackageUri(parentUri);
return a.Equals(b);
} Type guard
static bool IsSamePackageUri(Uri candidate, Uri reference) =>
candidate != null && reference != null &&
System.IO.Packaging.PackUriHelper.GetPackageUri(candidate).Equals(
System.IO.Packaging.PackUriHelper.GetPackageUri(reference)); Try / catch
try
{
xamlObject = loader.Load(stream, parentUri, parserContext);
}
catch (FileFormatException ex)
{
// handle cross-package URI violation: repair BaseUri or re-package
} Prevention
- Keep every XPS part and its references inside a single package
- Build BaseUri with PackUriHelper.Create for the correct package
- Never mix pack://application and pack://container URIs for XPS parts
- Validate package relationships before loading
When it happens
Trigger: Loading XAML in an XPS-document context where PackUriHelper.GetPackageUri(parentUri) differs from the package URI of the part being loaded (XPSS0ValidatingLoader.cs:99), e.g. a BaseUri pointing at pack://application while the part lives in pack://container.
Common situations: XPS documents with relationships targeting parts in an embedded/foreign package; mismatched pack URIs after merging packages; content that mixes application resources and XPS container parts; hand-built pack URIs with the wrong package component.
Understand the failure class
Background: "Invalid URL" / "URL cannot be empty": fix the malformed or missing URL behind request-construction failures — this error's family across 50 libraries.
Related errors
- Resource_XpsPackageBoundaryViolation
- SR.XpsValidatingLoaderDiscardControlHasIncorrectType
- SR.XpsValidatingLoaderDuplicateReference
- SR.XpsValidatingLoaderMoreThanOneThumbnailInPackage
- SR.XpsValidatingLoaderUnlistedResource
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/5c0229f3b3434e11.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Documents/XPSS0ValidatingLoader.cs:99
// Publish the active XPS package URI so that PresentationCore
// sinks (BitmapDecoder, ColorContext) can enforce same-package
// containment without depending on PresentationFramework.
// Nesting-safe: save and restore any previously-active value.
XpsLoadingContext.ActivePackageUri = packageUri;
try
{
Package package = PreloadedPackages.GetPackage(packageUri);
Uri parentPackageUri = null;
if (parentUri != null)
{
parentPackageUri = PackUriHelper.GetPackageUri(parentUri);
if (!parentPackageUri.Equals(packageUri))
{
throw new FileFormatException(SR.XpsValidatingLoaderUriNotInSamePackage);
}
}
if (package == null)
{
if (rootElement == null)
{
XmlReader reader = XmlReader.Create(stream, null, pc);
obj = XamlReader.Load(reader, pc, XamlParseMode.Synchronous, true, safeTypes);
stream.Close();
}
return obj;
}
schema.ValidateRelationships(package, packageUri, partUri, mimeType);
if (schema.AllowsMultipleReferencesToSameUri(mimeType))View on GitHub (pinned to 81131a70a4)