dotnet/wpf · error · FileFormatException
SR.XpsValidatingLoaderUnsupportedEncoding
Error message
SR.XpsValidatingLoaderUnsupportedEncoding
What it means
The XPS validating loader only accepts XML parts declared (or detected) as UTF-8 or UTF-16. During Read(), when the XML declaration carries an encoding attribute other than 'utf-8' or 'unicode' (UTF-16), a FileFormatException is thrown because XPS (OPC) mandates one of those two encodings for XML markup parts.
Solutions
- Re-save the part's XML stream as UTF-8 (or UTF-16) so the XML declaration matches (e.g. XmlWriter with Encoding.UTF8).
- Remove the bogus encoding attribute or fix it to encoding="utf-8" in the XML declaration.
- Strip BOM/declaration mismatches: ensure the stream's actual byte encoding matches the declared encoding.
- Regenerate the XPS document from the source application rather than editing markup by hand.
Example fix
// before
var settings = new XmlWriterSettings { Encoding = Encoding.GetEncoding("windows-1252") };
// after
var settings = new XmlWriterSettings { Encoding = new UTF8Encoding(false) }; Defensive patterns
Strategy: validation
Validate before calling
using var sr = new StreamReader(part.GetStream());
string head = sr.ReadLine();
var m = System.Text.RegularExpressions.Regex.Match(head ?? "", "encoding=\"([^\"]+)\"");
if (m.Success && !m.Groups[1].Value.Equals("utf-8", StringComparison.OrdinalIgnoreCase) &&
!m.Groups[1].Value.Equals("unicode", StringComparison.OrdinalIgnoreCase))
throw new InvalidDataException($"Unsupported XPS part encoding: {m.Groups[1].Value}"); Type guard
static bool IsSupportedXpsEncoding(XmlReader r) =>
r.Encoding is UTF8Encoding || r.Encoding is UnicodeEncoding; Try / catch
try { loader.Load(stream); }
catch (FileFormatException ex) when (ex.Message.Contains("encoding")) { /* re-encode part to UTF-8 and retry, or surface a clear message */ } Prevention
- Always write XPS XML parts with UTF8Encoding (no BOM surprises) via XmlWriterSettings.Encoding.
- Never hand-edit .fpage/.fdseq markup in editors that save ANSI.
- After package edits, re-verify each XML part's declaration and BOM.
- Test XPS output with the validating loader before distribution.
When it happens
Trigger: Calling code that opens an XPS/OPC package part whose XML starts with an XML declaration declaring e.g. encoding="utf-16le", "iso-8859-1", "windows-1252" or "us-ascii"; the XpsSchemaValidator's XmlEncodingEnforcingTextReader.Read() checks the declaration's encoding attribute on the first node read and throws.
Common situations: Non-WPF tools generating XPS markup with a default XML serializer encoding; hand-edited FixedPage .fpage files saved with a regional codepage; conversion pipelines that re-serialize XPS parts with XmlWriter configured for a non-UTF-8 encoding.
Understand the failure class
Background: UnsupportedOperationException and "is not supported" errors: when a library deliberately refuses a call — this error's family across 30 libraries.
Related errors
- " }} " element found. Expected fixed page element ( }} ).
- SR.EncodingNotSupported
- SR.InvalidDSContentType
- SR.InvalidSFContentType
- SR.InvalidStoryFragmentsMarkup
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/0cf3f295559f3b55.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Documents/FixedSchema.cs:45
{
}
public override bool Read()
{
bool result = base.Read();
if (result && !_encodingChecked)
{
if (base.NodeType == XmlNodeType.XmlDeclaration)
{
string encoding = base["encoding"];
if (encoding != null)
{
if (!encoding.Equals(Encoding.Unicode.WebName, StringComparison.OrdinalIgnoreCase) &&
!encoding.Equals(Encoding.UTF8.WebName, StringComparison.OrdinalIgnoreCase))
{
throw new FileFormatException(SR.XpsValidatingLoaderUnsupportedEncoding);
}
}
}
if (!(base.Encoding is UTF8Encoding) && !(base.Encoding is UnicodeEncoding))
{
throw new FileFormatException(SR.XpsValidatingLoaderUnsupportedEncoding);
}
_encodingChecked = true;
}
return result;
}
private bool _encodingChecked;
}
View on GitHub (pinned to 81131a70a4)