{"record":{"id":"6e561b960bb144f7","repo":"SubtitleEdit/subtitleedit","slug":"custom-format-template-is-empty-xmlpath","errorCode":null,"errorMessage":"Custom format template is empty: {xmlPath}","messagePattern":"Custom format template is empty: (.+?)","errorType":"exception","errorClass":"InvalidDataException","httpStatus":null,"severity":"error","filePath":"src/seconv/Core/CustomFormatTemplateLoader.cs","lineNumber":38,"sourceCode":"        public string FormatParagraph { get; set; } = string.Empty;\n        public string FormatFooter { get; set; } = string.Empty;\n        public string FormatTimeCode { get; set; } = string.Empty;\n        public string? FormatNewLine { get; set; }\n    }\n\n    public static CustomFormatTemplate Load(string xmlPath)\n    {\n        if (!File.Exists(xmlPath))\n        {\n            throw new FileNotFoundException($\"Custom format template not found: {xmlPath}\", xmlPath);\n        }\n\n        try\n        {\n            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}","sourceCodeStart":20,"sourceCodeEnd":56,"githubUrl":"https://github.com/SubtitleEdit/subtitleedit/blob/17a9f0748781032255db3526b7215d2fb891e3af/src/seconv/Core/CustomFormatTemplateLoader.cs#L20-L56","documentation":"Thrown when XmlSerializer.Deserialize returns null for the custom-format template — the file exists and opened for reading but deserialised to no object. This indicates an XML document that is structurally empty or whose root does not map to the XmlShape ('CustomFormatItem' root). Treated as corrupt/empty input.","triggerScenarios":"Calling Load on an existing but empty file, or an XML whose content the XmlSerializer maps to null (e.g. an effectively empty <CustomFormatItem/> that yields a null cast at line 37-38). Distinct from a parse error (error 97) which is a thrown exception.","commonSituations":"A zero-byte or whitespace-only .xml file; an XML with a different root element that the serializer silently skips; a template exported incorrectly by another tool producing an empty document.","solutions":["Open the XML and confirm it has a <CustomFormatItem> root with the expected child elements (Name, Extension, FormatHeader, FormatParagraph, FormatFooter, FormatTimeCode).","If the file is empty, regenerate it from a working custom-format export.","Provide a minimal valid template body and re-save."],"exampleFix":"<!-- before: empty file -->\n\n<!-- after: minimal valid template -->\n<CustomFormatItem>\n  <Name>MyFormat</Name>\n  <Extension>.txt</Extension>\n  <FormatHeader></FormatHeader>\n  <FormatParagraph>{start} {text}</FormatParagraph>\n  <FormatFooter></FormatFooter>\n  <FormatTimeCode>HH:mm:ss,fff</FormatTimeCode>\n</CustomFormatItem>","handlingStrategy":"validation","validationCode":"// Reject empty/whitespace-only files before deserialising.\nvar info = new FileInfo(xmlPath);\nif (info.Length == 0 || string.IsNullOrWhiteSpace(File.ReadAllText(xmlPath)))\n    return Error($\"Custom format template is empty: {xmlPath}\");","typeGuard":null,"tryCatchPattern":"try { tpl = CustomFormatTemplateLoader.Load(xmlPath); }\ncatch (InvalidDataException ex) when (ex.Message.Contains(\"is empty\"))\n{ Console.Error.WriteLine(ex.Message); return; }","preventionTips":["Pre-check the file is non-empty and well-formed before loading.","Validate the XML has a <CustomFormatItem> root with the expected child elements.","Regenerate templates from a known-good export rather than hand-creating empty files."],"tags":["custom-format","xml","empty-file","template"],"backgroundTag":null,"analyzedSha":"17a9f0748781032255db3526b7215d2fb891e3af","analyzedAt":"2026-08-13T18:11:43.374Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}