dotnet/wpf · error · FileFormatException

SR.XpsValidatingLoaderDiscardControlHasIncorrectType

Error message

SR.XpsValidatingLoaderDiscardControlHasIncorrectType

What it means

The XPS validating loader found a FixedPage's DiscardControl part whose content type does not match the required discard-control MIME type. The loader validates every part referenced via the discard-control relationship and throws FileFormatException to reject malformed or tampered XPS packages rather than loading them with an invalid payload part.

Solutions

  1. Regenerate or fix the XPS package so the DiscardControl part's content type matches the required discard-control content type in [Content_Types].xml.
  2. Open the package and inspect the target part's ContentType; correct the Override/Default entry in [Content_Types].xml.
  3. Remove the DiscardControl relationship if discarding is not needed, so validation skips that part.
  4. Catch FileFormatException at load time and treat the document as invalid/re-fetch a clean copy.

Example fix

// before ([Content_Types].xml missing/wrong override for /Documents/1/Page1.fpage/_rels/discard.xml)
<Default Extension="xml" ContentType="application/octet-stream" />
// after
<Override PartName="/Documents/1/Page1.fpage/_rels/discard.xml" ContentType="application/vnd.ms-package.discard-control+xml" />
Defensive patterns

Strategy: validation

Validate before calling

static bool HasValidDiscardControl(Package package, Uri partUri, string discardTarget)
{
    var target = PackUriHelper.ResolvePartUri(partUri, new Uri(discardTarget, UriKind.Relative));
    return package.PartExists(target) &&
        string.Equals(new ContentType(package.GetPart(target).ContentType).ToString(), "application/vnd.ms-package.discard-control+xml", StringComparison.OrdinalIgnoreCase);
}

Try / catch

try { var doc = new XpsDocument(pkgPath, FileAccess.Read); }
catch (FileFormatException ex) { /* reject/repair malformed package */ }

Prevention

When it happens

Trigger: Loading an XPS document (XpsDocument/FixedDocumentSequence) whose FixedPage has a DiscardControl relationship targeting a part whose ContentType is not the required discard-control type/subtype — e.g. the part was renamed, rewritten by a custom packaging tool, or points at the wrong part.

Common situations: Hand-built or third-party-generated XPS packages with wrong ContentTypes entries in [Content_Types].xml; files edited/re-zipped by scripts that change part names; corrupted downloads where a different part replaced the discard control part.

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/1f403b90f57f08d7. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Documents/FixedSchema.cs:631

                checkRels = package.GetRelationshipsByType(_discardControlRel);
                count = 0;
                foreach (PackageRelationship rel in checkRels)
                {
                    count++;
                    if (count > 1)
                    {
                        throw new FileFormatException(SR.XpsValidatingLoaderMoreThanOneDiscardControlInPackage);
                    }

                    // Also check for existence and type
                    Uri targetUri = PackUriHelper.ResolvePartUri(partUri, rel.TargetUri);
                    Uri absTargetUri = PackUriHelper.Create(packageUri, targetUri);

                    PackagePart targetPart = package.GetPart(targetUri);

                    if (!_discardControlContentType.AreTypeAndSubTypeEqual(new ContentType(targetPart.ContentType)))
                    {
                        throw new FileFormatException(SR.XpsValidatingLoaderDiscardControlHasIncorrectType);
                    }
                }

                // This is the XPS payload root part. We also should check if the Package only has at most one thumbnail...
                checkRels = package.GetRelationshipsByType(_thumbnailRel);
                count = 0;
                foreach (PackageRelationship rel in checkRels)
                {
                    count++;
                    if (count > 1)
                    {
                        throw new FileFormatException(SR.XpsValidatingLoaderMoreThanOneThumbnailInPackage);
                    }

                    // Also check for existence and type
                    Uri targetUri = PackUriHelper.ResolvePartUri(partUri, rel.TargetUri);
                    Uri absTargetUri = PackUriHelper.Create(packageUri, targetUri);

View on GitHub (pinned to 81131a70a4)