{"record":{"id":"e007778d73e07012","repo":"dotnet/wpf","slug":"sr-reachpackaging-partfromdifferentcontainer","errorCode":null,"errorMessage":"SR.ReachPackaging_PartFromDifferentContainer","messagePattern":"SR\\.ReachPackaging_PartFromDifferentContainer","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/ReachFramework/Packaging/XpsManager.cs","lineNumber":1475,"sourceCode":"            // won't be removed from the package\n            //\n\n            if (package.FileOpenAccess == FileAccess.Read)\n            {\n                throw new IOException(SR.ReachPackaging_CannotModifyReadOnlyContainer);\n            }\n\n            //\n            // Throw If the part provided is null\n            //\n            ArgumentNullException.ThrowIfNull(startingPart);\n\n            //\n            // Throw If the part provided is from a different container\n            //\n            if (startingPart.Package != package)\n            {\n                throw new ArgumentException(SR.ReachPackaging_PartFromDifferentContainer);\n            }\n\n                package.CreateRelationship(startingPart.Uri, TargetMode.Internal, XpsS0Markup.ReachPackageStartingPartRelationshipType);\n        }\n\n        #endregion Internal static methods\n\n        #region Public static methods\n\n        /// <summary>\n        /// This method generates a relative URI path based on the base URI\n        /// and the absolute URI.\n        /// </summary>\n        /// <param name=\"baseUri\">\n        /// The base uri for the part.\n        /// </param>\n        /// <param name=\"fileUri\">\n        /// The absolute path URI to be converted to relative.","sourceCodeStart":1457,"sourceCodeEnd":1493,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/ReachFramework/Packaging/XpsManager.cs#L1457-L1493","documentation":"When registering a starting part, XpsManager verifies that startingPart.Package equals the target package; if the part belongs to a different container, ArgumentException(SR.ReachPackaging_PartFromDifferentContainer) is thrown. A package relationship can only target parts within the same OPC package.","triggerScenarios":"Calling the starting-part API with an XpsFixedDocumentSequenceReader/part obtained from a different XpsDocument/Package instance than the package being modified — e.g. mixing documents opened from two files or streams.","commonSituations":"Copying content between two XpsDocuments and passing the source document's sequence part to the target package; holding a cached FixedDocumentSequence reference after opening a new XpsDocument.","solutions":["Pass the starting part obtained from the same XpsDocument/Package instance you are modifying.","Re-acquire the FixedDocumentSequence part from the target package instead of reusing one from another container.","If you intended to copy content, create an equivalent part inside the target package and use that as the starting part."],"exampleFix":"// before\notherDoc.Package.CreateRelationship(seqFromDocA.Uri, TargetMode.Internal, XpsS0Markup.ReachPackageStartingPartRelationshipType);\n// after\nvar seqFromDocA = docA.GetFixedDocumentSequence();\n// use the part belonging to the package being modified:\nvar seqOfTarget = targetDoc.GetFixedDocumentSequence();\ntargetDoc.Package.CreateRelationship(seqOfTarget.Uri, TargetMode.Internal, XpsS0Markup.ReachPackageStartingPartRelationshipType);","handlingStrategy":"validation","validationCode":"if (startingPart.Package != package) throw new ArgumentException(\"Part belongs to a different package\");","typeGuard":"bool SamePackage(PackagePart part, Package pkg) => part.Package == pkg;","tryCatchPattern":"catch (ArgumentException) { /* wrong container: re-acquire the part from the target package */ }","preventionTips":["Always obtain parts from the same XpsDocument instance you modify.","Avoid caching part references across document lifetimes."],"tags":["xps","packaging","cross-container","argument-validation","wpf"],"backgroundTag":"incompatible-source-type","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"}