{"record":{"id":"71287eff7e8e3103","repo":"SubtitleEdit/subtitleedit","slug":"bdn-xml-document-could-not-be-created","errorCode":null,"errorMessage":"BDN XML document could not be created.","messagePattern":"BDN XML document could not be created\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"warning","filePath":"src/libuilogic/Export/ExportHandlerBdnXml.cs","lineNumber":177,"sourceCode":"        {\n            _last = new Paragraph(string.Empty, 0, 0);\n        }\n\n        var doc = new XmlDocument();\n        doc.LoadXml(\"<?xml version=\\\"1.0\\\" encoding=\\\"UTF-8\\\"?>\" + Environment.NewLine +\n                    \"<BDN Version=\\\"0.93\\\" xmlns:xsi=\\\"http://www.w3.org/2001/XMLSchema-instance\\\" xsi:noNamespaceSchemaLocation=\\\"BD-03-006-0093b BDN File Format.xsd\\\">\" + Environment.NewLine +\n                    \"<Description>\" + Environment.NewLine +\n                    \"<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        {","sourceCodeStart":159,"sourceCodeEnd":195,"githubUrl":"https://github.com/SubtitleEdit/subtitleedit/blob/17a9f0748781032255db3526b7215d2fb891e3af/src/libuilogic/Export/ExportHandlerBdnXml.cs#L159-L195","documentation":"Thrown by ExportHandlerBdnXml after loading a hardcoded, valid BDN XML string via XmlDocument.LoadXml. The check guards against doc or doc.DocumentElement being null, which cannot happen with this specific literal string unless LoadXml throws first (in which case this line is never reached). This is a defensive, effectively-unreachable assertion.","triggerScenarios":"Only reachable if XmlDocument.LoadXml somehow returned a doc with a null DocumentElement for well-formed XML - a scenario the .NET XML parser does not produce. LoadXml would throw XmlException on malformed input before reaching the null check.","commonSituations":"Not expected to occur in practice. If it does, it indicates a corrupted .NET runtime, a custom XmlDocument subclass, or post-LoadXml reflection that nulled DocumentElement.","solutions":["Treat as an assertion failure: capture a full stack trace and the runtime version.","Verify the codebase has not subclassed XmlDocument or tampered with doc after LoadXml.","If reproducible, replace the literal-string LoadXml with a programmatic build to isolate the cause."],"exampleFix":null,"handlingStrategy":"validation","validationCode":"// The check is defensive over a hardcoded literal; in practice just assert after load\nvar doc = new XmlDocument();\ndoc.LoadXml(bdnLiteral);\nif (doc.DocumentElement == null)\n    throw new InvalidOperationException(\"BDN XML failed to load a root element (unexpected - capture runtime info).\");","typeGuard":null,"tryCatchPattern":"// Unreachable in practice; treat any occurrence as a bug to investigate\ntry\n{\n    exportHandler.Export();\n}\ncatch (InvalidOperationException ex) when (ex.Message == \"BDN XML document could not be created.\")\n{\n    logger.Critical(ex, \"Defensive BDN XML assertion fired - capture full environment details.\");\n    throw;\n}","preventionTips":["Treat this throw as an assertion: if it fires, investigate the runtime/build, not normal operation.","Do not edit the BDN XML literal without re-confirming it loads with a root element.","Add a unit test that loads the literal and asserts doc.DocumentElement != null to guard refactors."],"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"}