{"record":{"id":"7971b6354723be27","repo":"dotnet/wpf","slug":"sr-invalidstoryfragmentsmarkup","errorCode":null,"errorMessage":"SR.InvalidStoryFragmentsMarkup","messagePattern":"SR\\.InvalidStoryFragmentsMarkup","errorType":"exception","errorClass":"FileFormatException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Documents/FixedDocument.cs","lineNumber":914,"sourceCode":"            if (baseUri.Scheme.Equals(PackUriHelper.UriSchemePack, StringComparison.OrdinalIgnoreCase))\n            {\n                // avoid the case of pack://application,,,\n                if (!baseUri.Host.Equals(BaseUriHelper.PackAppBaseUri.Host) &&\n                    !baseUri.Host.Equals(BaseUriHelper.SiteOfOriginBaseUri.Host))\n                {\n                    Uri structureUri = GetStructureUriFromRelationship(baseUri, _storyFragmentsRelationshipName);\n\n                    if (structureUri != null)\n                    {\n                        ContentType mimeType;\n                        o = ValidateAndLoadPartFromAbsoluteUri(structureUri, false, null, out mimeType);\n                        if (!_storyFragmentsContentType.AreTypeAndSubTypeEqual(mimeType))\n                        {\n                            throw new FileFormatException(SR.InvalidSFContentType);\n                        }\n                        if (!(o is StoryFragments))\n                        {\n                            throw new FileFormatException(SR.InvalidStoryFragmentsMarkup);\n                        }\n                    }\n                }\n            }\n\n            return o as StoryFragments;\n        }\n\n\n        private static object ValidateAndLoadPartFromAbsoluteUri(Uri AbsoluteUriDoc, bool validateOnly, string rootElement, out ContentType mimeType)\n        {\n            mimeType = null;\n            Stream pageStream = null;\n            object o = null;\n\n            try\n            {\n                pageStream = WpfWebRequestHelper.CreateRequestAndGetResponseStream(AbsoluteUriDoc, out mimeType);","sourceCodeStart":896,"sourceCodeEnd":932,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Documents/FixedDocument.cs#L896-L932","documentation":"FixedDocument.GetStoryFragments reads the StoryFragments part from an XPS package. After verifying the part's content type is the expected StoryFragments MIME type, it deserializes the part and checks that the resulting object is a StoryFragments instance. If the markup inside the part does not deserialize into StoryFragments, the document throws FileFormatException with SR.InvalidStoryFragmentsMarkup, because a fixed document cannot interpret annotation/print-ticket story data from malformed markup.","triggerScenarios":"Loading an XPS/FixedDocument whose StoryFragments package part exists and declares the correct content type, but whose XML body is not valid StoryFragments markup (wrong root element, foreign markup, or a deserialize that yields something other than StoryFragments).","commonSituations":"Hand-edited or third-party-generated XPS files with a mislabeled StoryFragments part; tools that copy a generic XML part into a .fragments slot; corrupted or truncated document packages.","solutions":["Open the XPS package, locate the StoryFragments part, and validate its root element is <StoryFragments> in the correct XPS namespace.","Regenerate the XPS document with a compliant producer instead of hand-editing package parts.","If custom markup is intentional, remove or relocate it to a part where foreign content is permitted.","Wrap loading in a FileFormatException handler and fall back to loading the document without story fragments if the application can tolerate their absence."],"exampleFix":"// before: trusting a third-party XPS file blindly\nvar doc = new FixedDocument();\ndoc.SetSource(xpsStream);\n\n// after: validate the package part first\nusing var pkg = Package.Open(xpsStream);\nvar sfPart = pkg.GetPart(new Uri(\"/Documents/1/StoryFragments.xml\", UriKind.Relative));\nstring xml = new StreamReader(sfPart.GetStream()).ReadToEnd();\nif (!xml.TrimStart().StartsWith(\"<StoryFragments\"))\n    throw new InvalidDataException(\"StoryFragments part contains invalid markup.\");\nvar doc = new FixedDocument();\ndoc.SetSource(xpsStream);","handlingStrategy":"validation","validationCode":"bool IsStoryFragmentsPartValid(Package pkg)\n{\n    var part = pkg.GetPart(new Uri(\"/Documents/1/StoryFragments.xml\", UriKind.Relative));\n    using var reader = XmlReader.Create(part.GetStream());\n    reader.MoveToContent();\n    return reader.NodeType == XmlNodeType.Element\n        && reader.LocalName == \"StoryFragments\"\n        && reader.NamespaceURI == \"http://schemas.microsoft.com/xps/2005/06\";\n}","typeGuard":"static bool IsStoryFragments(object o) => o is StoryFragments;","tryCatchPattern":"try { doc.SetSource(stream); }\ncatch (FileFormatException ex) when (ex.Message.Contains(\"StoryFragments\"))\n{\n    // reload without story fragments or surface a friendly \"corrupt XPS\" message\n}","preventionTips":["Never hand-edit XPS package parts; regenerate with a compliant producer.","Validate third-party XPS files against the OpenXPS/XPS schema before loading.","Keep StoryFragments content in the correctly typed package part with the right content type."],"tags":["xps","file-format","markup-validation","fixeddocument"],"backgroundTag":"schema-validation-failed","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-22T01:17:13.364Z"}