{"record":{"id":"3f5f5dc2dd503373","repo":"SubtitleEdit/subtitleedit","slug":"failed-to-parse-custom-format-xml-at-xmlpath-e","errorCode":null,"errorMessage":"Failed to parse custom-format XML at {xmlPath}: {ex.Message}","messagePattern":"Failed to parse custom-format XML at (.+?): (.+?)","errorType":"exception","errorClass":"InvalidDataException","httpStatus":null,"severity":"error","filePath":"src/seconv/Core/CustomFormatTemplateLoader.cs","lineNumber":53,"sourceCode":"            using var fs = File.OpenRead(xmlPath);\n            var serializer = new XmlSerializer(typeof(XmlShape));\n            var shape = (XmlShape?)serializer.Deserialize(fs)\n                ?? throw new InvalidDataException($\"Custom format template is empty: {xmlPath}\");\n\n            return new CustomFormatTemplate\n            {\n                Name = shape.Name,\n                Extension = shape.Extension,\n                FormatHeader = shape.FormatHeader,\n                FormatParagraph = shape.FormatParagraph,\n                FormatFooter = shape.FormatFooter,\n                FormatTimeCode = shape.FormatTimeCode,\n                FormatNewLine = shape.FormatNewLine,\n            };\n        }\n        catch (Exception ex) when (ex is not FileNotFoundException && ex is not InvalidDataException)\n        {\n            throw new InvalidDataException($\"Failed to parse custom-format XML at {xmlPath}: {ex.Message}\", ex);\n        }\n    }\n}\n","sourceCodeStart":35,"sourceCodeEnd":57,"githubUrl":"https://github.com/SubtitleEdit/subtitleedit/blob/17a9f0748781032255db3526b7215d2fb891e3af/src/seconv/Core/CustomFormatTemplateLoader.cs#L35-L57","documentation":"Thrown when deserialising the custom-format XML raised an exception other than FileNotFoundException/InvalidDataException (XmlException, InvalidOperationException from the serializer, IOException, etc.). The loader catches it and re-throws as InvalidDataException preserving the original as InnerException, so callers get one exception type for all malformed-template cases.","triggerScenarios":"Calling Load on an XML that is not well-formed (unbalanced tags, bad encoding, invalid characters) or whose structure violates the XmlShape contract — the catch at line 51 wraps the serializer's XmlException/InvalidOperationException into InvalidDataException at line 53.","commonSituations":"Hand-edited XML with a typo (unclosed tag, stray &); a template saved in the wrong encoding (BOM/UTF-16); an XML containing characters illegal in the target element types; a template copied from a newer/different schema.","solutions":["Validate the XML well-formedness in an editor or xmllint.","Inspect InnerException for the exact line/reason reported by XmlSerializer.","Ensure the file is UTF-8 (with or without BOM) and matches the XmlShape schema (root <CustomFormatItem>).","Regenerate the template from a known-good export and diff against the broken one."],"exampleFix":"// before\nvar tpl = CustomFormatTemplateLoader.Load(path);\n\n// after\nCustomFormatTemplate tpl;\ntry { tpl = CustomFormatTemplateLoader.Load(path); }\ncatch (InvalidDataException ex) { Console.Error.WriteLine($\"Bad template: {ex.Message} (root: {ex.InnerException?.Message})\"); return; }","handlingStrategy":"try-catch","validationCode":"// Validate XML well-formedness before delegating to the loader.\ntry { var doc = XDocument.Load(xmlPath); }  // throws XmlException if malformed\ncatch (XmlException) { return Error($\"Malformed XML: {xmlPath}\"); }","typeGuard":null,"tryCatchPattern":"try { tpl = CustomFormatTemplateLoader.Load(xmlPath); }\ncatch (InvalidDataException ex) { Console.Error.WriteLine($\"Bad template: {ex.Message} (root: {ex.InnerException?.Message})\"); return; }","preventionTips":["Validate the XML with XDocument.Load / xmllint before passing to Load.","Inspect InnerException for the exact serializer error (line/position).","Save templates as UTF-8 matching the XmlShape schema, and regenerate from known-good exports."],"tags":["custom-format","xml","parse-error","template"],"backgroundTag":null,"analyzedSha":"17a9f0748781032255db3526b7215d2fb891e3af","analyzedAt":"2026-08-13T18:11:43.374Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}