{"record":{"id":"5046f596cf1968a3","repo":"dotnet/wpf","slug":"package-must-contain-an-xps-packagepart-5046f5","errorCode":null,"errorMessage":"Package must contain an XPS PackagePart.","messagePattern":"Package must contain an XPS PackagePart\\.","errorType":"exception","errorClass":"XpsPackagingException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/ReachFramework/Packaging/XpsFixedDocumentSequenceReaderWriter.cs","lineNumber":611,"sourceCode":"        EnsureThumbnail()\n        {\n            if( _thumbnail == null )\n            {\n                _thumbnail = CurrentXpsManager.EnsureThumbnail( this, _metroPart );\n            }\n        }\n\n        private\n        IXpsFixedDocumentReader\n        AddDocumentToCache(Uri documentUri)\n        {\n            //\n            // Retrieve the requested part from the package\n            //\n            PackagePart documentPart = CurrentXpsManager.GetPart(documentUri);\n            if (documentPart == null)\n            {\n                 throw new XpsPackagingException(SR.ReachPackaging_PartNotFound);\n            }\n\n            //\n            // If the part is not a fixed document then throw an exception\n            //\n            if (!documentPart.ValidatedContentType().AreTypeAndSubTypeEqual(XpsS0Markup.FixedDocumentContentType))\n            {\n                throw new XpsPackagingException(SR.ReachPackaging_NotAFixedDocument);\n            }\n\n            //\n            // Create the reader/writer for the part\n            //\n            IXpsFixedDocumentReader  fixedDocument = new XpsFixedDocumentReaderWriter(CurrentXpsManager, null, documentPart, _documentCache.Count+1);\n\n            //\n            // Cache the new reader/writer for later\n            //","sourceCodeStart":593,"sourceCodeEnd":629,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/ReachFramework/Packaging/XpsFixedDocumentSequenceReaderWriter.cs#L593-L629","documentation":"XpsPackagingException thrown by XpsFixedDocumentSequenceReaderWriter.AddDocumentToCache when a part at the given document URI cannot be retrieved from the XPS package (GetPart returned null). It means the FixedDocumentSequence references a document part that does not physically exist in the package. The library throws it while parsing documents to fail fast instead of returning a null reader.","triggerScenarios":"Reading/consuming an XPS package whose FixedDocumentSequence contains a document URI that has no corresponding PackagePart — e.g. a truncated, hand-edited, or corrupted .xps/.oxps file, or a package where relationships point to a part that was never written.","commonSituations":"Processing XPS files generated by third-party tools that wrote incomplete packages; opening a partially downloaded or truncated XPS; manually rebuilding an XPS zip and dropping a FixedDocument part; .NET WPF printing pipelines consuming malformed documents.","solutions":["Verify the package contains the FixedDocument part referenced by the FixedDocumentSequence (open the .xps as a zip and check /Documents/1/*.fpage paths).","Regenerate the XPS file from its original source application; do not hand-edit package contents.","Check for truncation — compare file size or re-download/copy the file.","If building packages yourself, ensure every document URI added to the sequence has a corresponding XpsFixedDocumentReaderWriter committed before closing the package.","Wrap XPS parsing in try-catch for XpsPackagingException and treat the file as corrupt, prompting re-export."],"exampleFix":"// before\nXpsDocument doc = new XpsDocument(path, FileAccess.Read);\nFixedDocumentSequenceReader seq = doc.FixedDocumentSequenceReader; // may throw downstream\n// after\ntry\n{\n    XpsDocument doc = new XpsDocument(path, FileAccess.Read);\n    var seq = doc.FixedDocumentSequenceReader;\n    if (seq == null) throw new InvalidDataException(\"No FixedDocumentSequence in package.\");\n}\ncatch (XpsPackagingException ex)\n{\n    Console.WriteLine($\"Corrupt XPS package: {ex.Message}\");\n}","handlingStrategy":"try-catch","validationCode":"// Before reading: confirm the referenced document parts exist\nusing (Package p = Package.Open(path, FileMode.Open, FileAccess.Read))\n{\n    foreach (PackageRelationship rel in packagePart.GetRelationshipsBySelector(\"http://schemas.microsoft.com/xps/2005/06/relativefrom/fixeddocumentsequence\"))\n    {\n        Uri resolved = PackUriHelper.ResolvePartUri(rel.SourceUri, rel.TargetUri);\n        if (!p.PartExists(resolved)) throw new InvalidDataException($\"Missing part: {resolved}\");\n    }\n}","typeGuard":"bool IsValidXpsPackage(string path) =>\n    File.Exists(path) && new FileInfo(path).Length > 0 &&\n    ZipFile.OpenRead(path).Entries.Any(e => e.FullName.EndsWith(\".fdoc\", StringComparison.OrdinalIgnoreCase));","tryCatchPattern":"try\n{\n    var seq = xpsDocument.FixedDocumentSequenceReader;\n}\ncatch (XpsPackagingException ex)\n{\n    // treat package as corrupt; fail with actionable message\n    throw new InvalidDataException($\"XPS package missing a referenced FixedDocument part: {ex.Message}\", ex);\n}","preventionTips":["Never hand-edit or re-zip XPS packages; always regenerate from the source app or the XpsDocument APIs.","Validate downloaded/copied .xps files (size, zip integrity) before processing.","Use XpsDocument API writers so every URI added to a sequence has a matching part.","Add a conformance pre-check step in batch pipelines that enumerates sequence relationships and verifies parts exist."],"tags":["xps","wpf","packaging","corrupt-file"],"backgroundTag":"resource-not-found","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"}