dotnet/wpf · error · FileFormatException
SR.EncodingNotSupported
Error message
SR.EncodingNotSupported
What it means
PackagingUtilities.PerformInitailReadAndVerifyEncoding reads the XML declaration of a package/piece stream and only accepts UTF-8 or Unicode (UTF-16) encodings explicitly declared via the encoding attribute. Any other declared encoding causes a FileFormatException(SR.EncodingNotSupported), because OPC packaging only supports these two encodings.
Solutions
- Re-save or convert the package content to UTF-8 (or UTF-16) encoding.
- Change the XML declaration to encoding="UTF-8" in the offending piece.
- Fix the upstream generator so it emits UTF-8/UTF-16 encoded package parts.
Example fix
// before <?xml version="1.0" encoding="ISO-8859-1"?> // after <?xml version="1.0" encoding="UTF-8"?>
Defensive patterns
Strategy: validation
Validate before calling
var decl = doc.FirstChild as XmlDeclaration;
string enc = decl?.Encoding;
if (enc != null && !enc.Equals("UTF-8", StringComparison.OrdinalIgnoreCase) && !enc.Equals("UTF-16", StringComparison.OrdinalIgnoreCase))
throw new InvalidOperationException("package encoding must be UTF-8 or UTF-16"); Try / catch
try { pkg = Package.Open(path); }
catch (FileFormatException ex) when (ex.Message.Contains("encoding")) { /* repair/re-encode file */ } Prevention
- Generate package parts as UTF-8 always.
- Never hand-edit XML declarations in OPC parts.
- Verify third-party generators emit UTF-8/UTF-16.
When it happens
Trigger: Opening a package file whose XML declaration carries an encoding attribute other than "UTF-8" or "UTF-16" (e.g. ISO-8859-1, ASCII, Windows-1252).
Common situations: Third-party tools or non-Windows generators producing .zip/.xml package pieces with a different declared encoding; hand-edited XML declarations; files saved by editors that default to other encodings.
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
- SR.MultipleRelationshipTransformsFound
- SR.PackageSignatureCorruption
- SR.PartReferenceUriMalformed
- SR.RequiredTagNotFound (template: RequiredTagNotFound, arg…
- SR.RequiredXmlAttributeMissing (uri)
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/5ce863992e4a0a82.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/Shared/MS/Internal/IO/Packaging/PackagingUtilities.cs:79
{
string encoding;
encoding = reader.GetAttribute(_encodingAttribute);
if (encoding != null && encoding.Length > 0)
{
encoding = encoding.ToUpperInvariant();
//If a non-empty encoding attribute is present [for example - <?xml version="1.0" encoding="utf-8" ?>]
//we check to see if the value is either "utf-8" or utf-16. Only these two values are supported
//Note: For Byte order markings that require additional information to be specified in
//the encoding attribute in XmlDeclaration have already been ruled out by this check as we allow for
//only two valid values.
if (string.Equals(encoding, _webNameUTF8, StringComparison.Ordinal)
|| string.Equals(encoding, _webNameUnicode, StringComparison.Ordinal))
return;
else
//if the encoding attribute has any other value we throw an exception
throw new FileFormatException(SR.EncodingNotSupported);
}
}
//if the XmlDeclaration is not present, or encoding attribute is not present, we
//base our decision on byte order marking. reader.Encoding will take that into account
//and return the correct value.
//Note: For Byte order markings that require additional information to be specified in
//the encoding attribute in XmlDeclaration have already been ruled out by the check above.
//Note: If not encoding attribute is present or no byte order marking is present the
//encoding default to UTF8
if (!(reader.Encoding is UnicodeEncoding || reader.Encoding is UTF8Encoding))
throw new FileFormatException(SR.EncodingNotSupported);
}
/// <summary>
/// VerifyStreamReadArgs
/// </summary>
/// <param name="s">stream</param>View on GitHub (pinned to 81131a70a4)