{"record":{"id":"0cf3f295559f3b55","repo":"dotnet/wpf","slug":"sr-xpsvalidatingloaderunsupportedencoding","errorCode":null,"errorMessage":"SR.XpsValidatingLoaderUnsupportedEncoding","messagePattern":"SR\\.XpsValidatingLoaderUnsupportedEncoding","errorType":"exception","errorClass":"FileFormatException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Documents/FixedSchema.cs","lineNumber":45,"sourceCode":"            {\n            }\n\n            public override bool Read()\n            {\n                bool result = base.Read();\n\n                if (result && !_encodingChecked)\n                {\n                    if (base.NodeType == XmlNodeType.XmlDeclaration)\n                    {\n                        string encoding = base[\"encoding\"];\n\n                        if (encoding != null)\n                        {\n                            if (!encoding.Equals(Encoding.Unicode.WebName, StringComparison.OrdinalIgnoreCase) &&\n                                        !encoding.Equals(Encoding.UTF8.WebName, StringComparison.OrdinalIgnoreCase))\n                            {\n                                throw new FileFormatException(SR.XpsValidatingLoaderUnsupportedEncoding);\n                            }\n                        }\n                    }\n\n                    if (!(base.Encoding is UTF8Encoding) && !(base.Encoding is UnicodeEncoding))\n                    {\n                        throw new FileFormatException(SR.XpsValidatingLoaderUnsupportedEncoding);\n                    }\n\n                    _encodingChecked = true;\n                }\n\n                return result;\n            }\n\n            private bool _encodingChecked;\n        }\n","sourceCodeStart":27,"sourceCodeEnd":63,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Documents/FixedSchema.cs#L27-L63","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"// before\nvar settings = new XmlWriterSettings { Encoding = Encoding.GetEncoding(\"windows-1252\") };\n// after\nvar settings = new XmlWriterSettings { Encoding = new UTF8Encoding(false) };","handlingStrategy":"validation","validationCode":"using var sr = new StreamReader(part.GetStream());\nstring head = sr.ReadLine();\nvar m = System.Text.RegularExpressions.Regex.Match(head ?? \"\", \"encoding=\\\"([^\\\"]+)\\\"\");\nif (m.Success && !m.Groups[1].Value.Equals(\"utf-8\", StringComparison.OrdinalIgnoreCase) &&\n    !m.Groups[1].Value.Equals(\"unicode\", StringComparison.OrdinalIgnoreCase))\n    throw new InvalidDataException($\"Unsupported XPS part encoding: {m.Groups[1].Value}\");","typeGuard":"static bool IsSupportedXpsEncoding(XmlReader r) =>\n    r.Encoding is UTF8Encoding || r.Encoding is UnicodeEncoding;","tryCatchPattern":"try { loader.Load(stream); }\ncatch (FileFormatException ex) when (ex.Message.Contains(\"encoding\")) { /* re-encode part to UTF-8 and retry, or surface a clear message */ }","preventionTips":["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."],"tags":["xps","encoding","xml","file-format"],"backgroundTag":"unsupported-operation","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-21T21:30:21.729Z"}