{"record":{"id":"3215f309b4a965e0","repo":"dotnet/wpf","slug":"fixedpage-has-more-than-one-related-story-fragment","errorCode":null,"errorMessage":"FixedPage has more than one related story fragment.","messagePattern":"FixedPage has more than one related story fragment\\.","errorType":"exception","errorClass":"XpsPackagingException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/ReachFramework/Packaging/XpsFixedPageReaderWriter.cs","lineNumber":853,"sourceCode":"                _metroPart.CreateRelationship(new Uri(resourcePath, UriKind.Relative),\n                                           TargetMode.Internal,\n                                           XpsS0Markup.ResourceRelationshipName);\n}\n\n            return xpsResource;\n        }\n\n        /// <summary>\n        /// </summary>\n        public\n        XpsStructure\n        AddStoryFragment(\n            )\n        {\n            if (this.StoryFragment != null)\n            {\n                // StoryFragments already available for this FixedPage\n                throw new XpsPackagingException(SR.ReachPackaging_MoreThanOneStoryFragment);\n            }\n\n            Uri pageUri = this.CurrentXpsManager.CreateFragmentUri(PageNumber);\n            //\n            // Create the part and writer\n            //\n            PackagePart metroPart = this.CurrentXpsManager.GeneratePart(\n                        XpsS0Markup.StoryFragmentsContentType,\n                        pageUri);\n\n            _storyFragment = new XpsStructure(CurrentXpsManager, this, metroPart);\n\n            //\n            // Create the relationship between the document and the document-structure\n            // Not in INode.Flush because IXpsFixedPageReader has no commit.\n            //\n            string storyFragmentPath = XpsManager.MakeRelativePath(this.Uri, _storyFragment.Uri);\n","sourceCodeStart":835,"sourceCodeEnd":871,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/ReachFramework/Packaging/XpsFixedPageReaderWriter.cs#L835-L871","documentation":"XpsPackagingException thrown by XpsFixedPageReaderWriter.AddStoryFragment when the page already has a StoryFragment. The XPS spec allows at most one StoryFragments part per FixedPage, so a second AddStoryFragment call is rejected. This keeps the package valid for consumers that read story (reading-order) data.","triggerScenarios":"Calling pageWriter.AddStoryFragment() twice on the same fixed page — commonly when generating story fragments in a loop per section but the page object is reused, or when a document template already attached a StoryFragment and generator code adds another.","commonSituations":"Document generators that produce multiple story sections and assume a new fragment per section; merging content from templates that already contain StoryFragments; refactored code paths where an earlier call to AddStoryFragment was forgotten.","solutions":["Call AddStoryFragment only once per page; write all story content into the single returned XpsStoryFragments writer.","Check page.StoryFragment for null before calling AddStoryFragment and reuse the existing writer.","Restructure so one page holds one story fragment; split content across pages if more fragments are needed.","If merging templates, strip pre-existing StoryFragments parts before adding your own.","Handle XpsPackagingException in batch pipelines to flag over-fragmented pages."],"exampleFix":"// before\nvar frag1 = page.AddStoryFragment();\nvar frag2 = page.AddStoryFragment(); // throws\n// after\nvar frag = page.StoryFragment ?? page.AddStoryFragment();\nfrag.AddStory(...); // all stories in one fragment\nfrag.Commit();","handlingStrategy":"validation","validationCode":"// Check before adding\nif (pageWriter.StoryFragment != null)\n{\n    var existing = pageWriter.StoryFragment; // reuse it\n}\nelse\n{\n    var frag = pageWriter.AddStoryFragment();\n}","typeGuard":"bool HasStoryFragment(IXpsFixedPageReaderWriter page) => page.StoryFragment != null;","tryCatchPattern":"try\n{\n    var frag = pageWriter.AddStoryFragment();\n}\ncatch (XpsPackagingException ex) when (ex.Message.Contains(\"StoryFragment\"))\n{\n    var existing = pageWriter.StoryFragment; // fall back to existing fragment\n}","preventionTips":["Call AddStoryFragment at most once per page; write all stories into the one fragment.","Always check page.StoryFragment before adding (template-merged pages may already have one).","Strip pre-existing StoryFragments when merging document templates.","Model story content as a list of stories within one fragment, not one fragment per story."],"tags":["xps","wpf","story-fragment","duplicate"],"backgroundTag":"file-already-exists","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"}