dotnet/wpf · error · FileFormatException

SR.XpsValidatingLoaderMoreThanOneDiscardControlInPackage

Error message

SR.XpsValidatingLoaderMoreThanOneDiscardControlInPackage

What it means

The XPS package (payload root, i.e. the FixedDocumentSequence level) may contain at most one DiscardControl part relationship. When ValidateRelationships enumerates package-level discard-control relationships on the start part and finds more than one, it throws FileFormatException.

Solutions

  1. Remove all but one package-level discard-control relationship (rebuild the DiscardControl part listing and re-create a single relationship).
  2. Merge discard information into a single DiscardControl part instead of multiple parts.
  3. Rebuild the package with a single discard-control relationship via a compliant XPS writer.
  4. Audit package-level GetRelationshipsByType(discardControlRel) count before loading.

Example fix

// before
pkg.CreateRelationship(discardUri1, TargetMode.Internal, discardRel);
pkg.CreateRelationship(discardUri2, TargetMode.Internal, discardRel); // second rel
// after
if (!pkg.GetRelationshipsByType(discardRel).Any())
    pkg.CreateRelationship(discardUri, TargetMode.Internal, discardRel);
Defensive patterns

Strategy: validation

Validate before calling

var discardRels = package.GetRelationshipsByType(discardControlRel).ToList();
if (discardRels.Count > 1)
    discardRels.Skip(1).ToList().ForEach(r => package.DeleteRelationship(r.Id));

Type guard

static bool HasAtMostOneDiscardControl(Package package, string relType) =>
    !package.GetRelationshipsByType(relType).Cast<PackageRelationship>().Skip(1).Any();

Try / catch

try { loader.Load(packageStream); }
catch (FileFormatException ex) { // multiple discard-control rels: rebuild single rel and retry
    RebuildSingleDiscardControlRel(packageStream); loader.Load(packageStream); }

Prevention

When it happens

Trigger: Opening an XPS package whose package-level relationships include two or more of the discard-control relationship type (http://schemas.microsoft.com/xps/2005/06/discard-control); the loop counter exceeds 1 during validation of the FixedDocumentSequence start part.

Common situations: Optimization/preprocessing tools that add a DiscardControl part on each run without removing the previous relationship; merged packages combining discard-control rels from multiple sources; scripts editing package-level relationships directly.

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/249ef67662ea1c59. Report an issue: GitHub.

Appendix: source

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

                            !_obfuscatedContentType.AreTypeAndSubTypeEqual(new ContentType(targetPart.ContentType)))
                    {
                        throw new FileFormatException(SR.XpsValidatingLoaderRestrictedFontHasIncorrectType);
                    }
                }
            }

            // check constraints for XPS fixed payload start part
            if (_fixedDocumentSequenceContentType.AreTypeAndSubTypeEqual(mimeType))
            {
                // This is the XPS payload root part. We also should check if the Package only has at most one discardcontrol...
                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)

View on GitHub (pinned to 81131a70a4)