{"record":{"id":"983585927183a38a","repo":"SubtitleEdit/subtitleedit","slug":"bdn-xml-events-node-not-found","errorCode":null,"errorMessage":"BDN XML Events node not found.","messagePattern":"BDN XML Events node not found\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"warning","filePath":"src/libuilogic/Export/ExportHandlerBdnXml.cs","lineNumber":185,"sourceCode":"                    \"<Name Title=\\\"subtitle_exp\\\" Content=\\\"\\\"/>\" + Environment.NewLine +\n                    \"<Language Code=\\\"eng\\\"/>\" + Environment.NewLine +\n                    \"<Format VideoFormat=\\\"\" + videoFormat + \"\\\" FrameRate=\\\"\" + _frameRate.ToString(CultureInfo.InvariantCulture) + \"\\\" DropFrame=\\\"false\\\"/>\" + Environment.NewLine +\n                    \"<Events Type=\\\"Graphic\\\" FirstEventInTC=\\\"\" + ToHHMMSSFF(_first.StartTime) + \"\\\" LastEventOutTC=\\\"\" + ToHHMMSSFF(_last.EndTime) + \"\\\" NumberofEvents=\\\"\" + _imagesSavedCount.ToString(CultureInfo.InvariantCulture) + \"\\\"/>\" + Environment.NewLine +\n                    \"</Description>\" + Environment.NewLine +\n                    \"<Events>\" + Environment.NewLine +\n                    \"</Events>\" + Environment.NewLine +\n                    \"</BDN>\");\n        if (doc == null || doc.DocumentElement == null)\n        {\n            throw new InvalidOperationException(\"BDN XML document could not be created.\");\n        }\n\n        XmlNode? events = doc.DocumentElement.SelectSingleNode(\"Events\");\n        doc.PreserveWhitespace = true;\n\n        if (events == null)\n        {\n            throw new InvalidOperationException(\"BDN XML Events node not found.\");\n        }\n        events.InnerXml = _sb.ToString();\n        FileUtil.WriteAllTextWithDefaultUtf8(Path.Combine(_folderName, \"index.xml\"), FormatUtf8Xml(doc));\n    }\n\n    private static string FormatUtf8Xml(XmlDocument doc)\n    {\n        var sb = new StringBuilder();\n        using (var writer = XmlWriter.Create(sb, new XmlWriterSettings { Indent = true, Encoding = Encoding.UTF8 }))\n        {\n            doc.Save(writer);\n        }\n        return sb.ToString().Replace(\" encoding=\\\"utf-16\\\"\", \" encoding=\\\"utf-8\\\"\").Trim(); // \"replace hack\" due to missing encoding (encoding only works if it's the only parameter...)\n    }\n\n    private string ToHHMMSSFF(TimeCode timeCode)\n    {\n        var ms = timeCode.TotalMilliseconds;","sourceCodeStart":167,"sourceCodeEnd":203,"githubUrl":"https://github.com/SubtitleEdit/subtitleedit/blob/17a9f0748781032255db3526b7215d2fb891e3af/src/libuilogic/Export/ExportHandlerBdnXml.cs#L167-L203","documentation":"Thrown by ExportHandlerBdnXml when SelectSingleNode(\"Events\") on the freshly loaded BDN document returns null. Like error 57, this guards a state that the hardcoded XML literal (which contains an <Events> element) cannot produce. It would only fire if the loaded XML's structure changed unexpectedly.","triggerScenarios":"After LoadXml of the literal that includes <Events></Events>, doc.DocumentElement.SelectSingleNode(\"Events\") returns null. Practically unreachable unless the literal is edited to remove <Events> or a custom resolver/namespace handling interferes with SelectSingleNode.","commonSituations":"Not expected in production. Could appear during refactoring if someone removes the <Events> element from the literal or changes namespace handling such that the default-namespace query no longer matches.","solutions":["Confirm the BDN XML literal still contains the <Events> element.","If namespaces are introduced, use an XmlNamespaceManager and a prefixed XPath in SelectSingleNode.","Treat as an assertion: capture the actual doc.OuterXml for diagnosis."],"exampleFix":null,"handlingStrategy":"validation","validationCode":"// Defensive check over a literal known to contain <Events>; guard the literal in a test\nvar doc = new XmlDocument();\ndoc.LoadXml(bdnLiteral);\nDebug.Assert(doc.SelectSingleNode(\"//Events\") != null, \"BDN literal must contain an Events node\");","typeGuard":null,"tryCatchPattern":"try\n{\n    exportHandler.Export();\n}\ncatch (InvalidOperationException ex) when (ex.Message == \"BDN XML Events node not found.\")\n{\n    logger.Critical(ex, \"Defensive BDN Events-node assertion fired - capture doc.OuterXml.\");\n    throw;\n}","preventionTips":["Keep the <Events> element in the BDN literal; add a unit test asserting it is present after load.","If introducing XML namespaces, switch SelectSingleNode to use an XmlNamespaceManager.","Treat any real occurrence as a regression in the literal, not a runtime input problem."],"tags":["defensive","xml","export","bdn","unreachable"],"backgroundTag":null,"analyzedSha":"17a9f0748781032255db3526b7215d2fb891e3af","analyzedAt":"2026-08-13T18:11:43.374Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}