{"record":{"id":"5ce863992e4a0a82","repo":"dotnet/wpf","slug":"sr-encodingnotsupported","errorCode":null,"errorMessage":"SR.EncodingNotSupported","messagePattern":"SR\\.EncodingNotSupported","errorType":"exception","errorClass":"FileFormatException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/Shared/MS/Internal/IO/Packaging/PackagingUtilities.cs","lineNumber":79,"sourceCode":"            {\n                string encoding;\n                encoding = reader.GetAttribute(_encodingAttribute);\n\n                if (encoding != null && encoding.Length > 0)\n                {\n                    encoding = encoding.ToUpperInvariant();\n\n                    //If a non-empty encoding attribute is present [for example - <?xml version=\"1.0\" encoding=\"utf-8\" ?>]\n                    //we check to see if the value is either \"utf-8\" or utf-16. Only these two values are supported\n                    //Note: For Byte order markings that require additional information to be specified in\n                    //the encoding attribute in XmlDeclaration have already been ruled out by this check as we allow for\n                    //only two valid values.\n                    if (string.Equals(encoding, _webNameUTF8, StringComparison.Ordinal)\n                        || string.Equals(encoding, _webNameUnicode, StringComparison.Ordinal))\n                        return;\n                    else\n                        //if the encoding attribute has any other value we throw an exception\n                        throw new FileFormatException(SR.EncodingNotSupported);\n                }\n            }\n\n            //if the XmlDeclaration is not present, or encoding attribute is not present, we\n            //base our decision on byte order marking. reader.Encoding will take that into account\n            //and return the correct value.\n            //Note: For Byte order markings that require additional information to be specified in\n            //the encoding attribute in XmlDeclaration have already been ruled out by the check above.\n            //Note: If not encoding attribute is present or no byte order marking is present the\n            //encoding default to UTF8\n            if (!(reader.Encoding is UnicodeEncoding || reader.Encoding is UTF8Encoding))\n                throw new FileFormatException(SR.EncodingNotSupported);\n        }\n\n        /// <summary>\n        /// VerifyStreamReadArgs\n        /// </summary>\n        /// <param name=\"s\">stream</param>","sourceCodeStart":61,"sourceCodeEnd":97,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/Shared/MS/Internal/IO/Packaging/PackagingUtilities.cs#L61-L97","documentation":"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.","triggerScenarios":"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).","commonSituations":"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.","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."],"exampleFix":"// before\n<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n// after\n<?xml version=\"1.0\" encoding=\"UTF-8\"?>","handlingStrategy":"validation","validationCode":"var decl = doc.FirstChild as XmlDeclaration;\nstring enc = decl?.Encoding;\nif (enc != null && !enc.Equals(\"UTF-8\", StringComparison.OrdinalIgnoreCase) && !enc.Equals(\"UTF-16\", StringComparison.OrdinalIgnoreCase))\n    throw new InvalidOperationException(\"package encoding must be UTF-8 or UTF-16\");","typeGuard":null,"tryCatchPattern":"try { pkg = Package.Open(path); }\ncatch (FileFormatException ex) when (ex.Message.Contains(\"encoding\")) { /* repair/re-encode file */ }","preventionTips":["Generate package parts as UTF-8 always.","Never hand-edit XML declarations in OPC parts.","Verify third-party generators emit UTF-8/UTF-16."],"tags":["opc","packaging","encoding","xml"],"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"}