{"record":{"id":"5c0229f3b3434e11","repo":"dotnet/wpf","slug":"sr-xpsvalidatingloaderurinotinsamepackage","errorCode":null,"errorMessage":"SR.XpsValidatingLoaderUriNotInSamePackage","messagePattern":"SR\\.XpsValidatingLoaderUriNotInSamePackage","errorType":"exception","errorClass":"FileFormatException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Documents/XPSS0ValidatingLoader.cs","lineNumber":99,"sourceCode":"                \n                // Publish the active XPS package URI so that PresentationCore\n                // sinks (BitmapDecoder, ColorContext) can enforce same-package\n                // containment without depending on PresentationFramework.\n                // Nesting-safe: save and restore any previously-active value.\n                XpsLoadingContext.ActivePackageUri = packageUri;\n\n                try\n                {\n                    Package package = PreloadedPackages.GetPackage(packageUri);\n\n                    Uri parentPackageUri = null;\n\n                    if (parentUri != null)\n                    {\n                        parentPackageUri = PackUriHelper.GetPackageUri(parentUri);\n                        if (!parentPackageUri.Equals(packageUri))\n                        {\n                            throw new FileFormatException(SR.XpsValidatingLoaderUriNotInSamePackage);\n                        }\n                    }\n\n                    if (package == null)\n                    {\n                        if (rootElement == null)\n                        {\n                            XmlReader reader = XmlReader.Create(stream, null, pc);\n                            obj = XamlReader.Load(reader, pc, XamlParseMode.Synchronous, true, safeTypes);\n                            stream.Close();\n                        }\n\n                        return obj;\n                    }\n\n                    schema.ValidateRelationships(package, packageUri, partUri, mimeType);\n\n                    if (schema.AllowsMultipleReferencesToSameUri(mimeType))","sourceCodeStart":81,"sourceCodeEnd":117,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Documents/XPSS0ValidatingLoader.cs#L81-L117","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"// before\npc.BaseUri = new Uri(\"pack://application:,,,/FixedPage.xaml\"); // different package\n\n// after\npc.BaseUri = new Uri(\"pack://container:,,,/Documents/1/Pages/1.fpage\"); // same package as part","handlingStrategy":"validation","validationCode":"static bool SamePackage(Uri baseUri, Uri parentUri)\n{\n    var a = System.IO.Packaging.PackUriHelper.GetPackageUri(baseUri);\n    var b = parentUri == null ? a : System.IO.Packaging.PackUriHelper.GetPackageUri(parentUri);\n    return a.Equals(b);\n}","typeGuard":"static bool IsSamePackageUri(Uri candidate, Uri reference) =>\n    candidate != null && reference != null &&\n    System.IO.Packaging.PackUriHelper.GetPackageUri(candidate).Equals(\n        System.IO.Packaging.PackUriHelper.GetPackageUri(reference));","tryCatchPattern":"try\n{\n    xamlObject = loader.Load(stream, parentUri, parserContext);\n}\ncatch (FileFormatException ex)\n{\n    // handle cross-package URI violation: repair BaseUri or re-package\n}","preventionTips":["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"],"tags":["xps","uri","pack-uri","file-format","validation"],"backgroundTag":"invalid-url","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-21T21:30:21.729Z"}