dotnet/wpf · error · FileFormatException

SR.InvalidStoryFragmentsMarkup

Error message

SR.InvalidStoryFragmentsMarkup

What it means

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.

Solutions

  1. Open the XPS package, locate the StoryFragments part, and validate its root element is <StoryFragments> in the correct XPS namespace.
  2. Regenerate the XPS document with a compliant producer instead of hand-editing package parts.
  3. If custom markup is intentional, remove or relocate it to a part where foreign content is permitted.
  4. Wrap loading in a FileFormatException handler and fall back to loading the document without story fragments if the application can tolerate their absence.

Example fix

// before: trusting a third-party XPS file blindly
var doc = new FixedDocument();
doc.SetSource(xpsStream);

// after: validate the package part first
using var pkg = Package.Open(xpsStream);
var sfPart = pkg.GetPart(new Uri("/Documents/1/StoryFragments.xml", UriKind.Relative));
string xml = new StreamReader(sfPart.GetStream()).ReadToEnd();
if (!xml.TrimStart().StartsWith("<StoryFragments"))
    throw new InvalidDataException("StoryFragments part contains invalid markup.");
var doc = new FixedDocument();
doc.SetSource(xpsStream);
Defensive patterns

Strategy: validation

Validate before calling

bool IsStoryFragmentsPartValid(Package pkg)
{
    var part = pkg.GetPart(new Uri("/Documents/1/StoryFragments.xml", UriKind.Relative));
    using var reader = XmlReader.Create(part.GetStream());
    reader.MoveToContent();
    return reader.NodeType == XmlNodeType.Element
        && reader.LocalName == "StoryFragments"
        && reader.NamespaceURI == "http://schemas.microsoft.com/xps/2005/06";
}

Type guard

static bool IsStoryFragments(object o) => o is StoryFragments;

Try / catch

try { doc.SetSource(stream); }
catch (FileFormatException ex) when (ex.Message.Contains("StoryFragments"))
{
    // reload without story fragments or surface a friendly "corrupt XPS" message
}

Prevention

When it happens

Trigger: 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).

Common situations: 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.

Understand the failure class

Background: Schema validation failed / invalid input schema: payload rejected because its shape doesn't match the expected schema — this error's family across 28 libraries.

Related errors


AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14). Data as JSON: /api/errors/7971b6354723be27. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Documents/FixedDocument.cs:914

            if (baseUri.Scheme.Equals(PackUriHelper.UriSchemePack, StringComparison.OrdinalIgnoreCase))
            {
                // avoid the case of pack://application,,,
                if (!baseUri.Host.Equals(BaseUriHelper.PackAppBaseUri.Host) &&
                    !baseUri.Host.Equals(BaseUriHelper.SiteOfOriginBaseUri.Host))
                {
                    Uri structureUri = GetStructureUriFromRelationship(baseUri, _storyFragmentsRelationshipName);

                    if (structureUri != null)
                    {
                        ContentType mimeType;
                        o = ValidateAndLoadPartFromAbsoluteUri(structureUri, false, null, out mimeType);
                        if (!_storyFragmentsContentType.AreTypeAndSubTypeEqual(mimeType))
                        {
                            throw new FileFormatException(SR.InvalidSFContentType);
                        }
                        if (!(o is StoryFragments))
                        {
                            throw new FileFormatException(SR.InvalidStoryFragmentsMarkup);
                        }
                    }
                }
            }

            return o as StoryFragments;
        }


        private static object ValidateAndLoadPartFromAbsoluteUri(Uri AbsoluteUriDoc, bool validateOnly, string rootElement, out ContentType mimeType)
        {
            mimeType = null;
            Stream pageStream = null;
            object o = null;

            try
            {
                pageStream = WpfWebRequestHelper.CreateRequestAndGetResponseStream(AbsoluteUriDoc, out mimeType);

View on GitHub (pinned to 81131a70a4)