{"record":{"id":"b9e9d9f979f5bb83","repo":"MaterialDesignInXAML/MaterialDesignInXamlToolkit","slug":"the-input-document-does-not-contain-a-root","errorCode":null,"errorMessage":"The input document does not contain a root","messagePattern":"The input document does not contain a root","errorType":"exception","errorClass":"InvalidDataException","httpStatus":null,"severity":"error","filePath":"src/MaterialDesignToolkit.ResourceGeneration/Program.cs","lineNumber":91,"sourceCode":"            GenerateOldXaml(GetXmlRoot(), true);\n        if (args.Contains(\"old\"))\n            GenerateOldXaml(GetXmlRoot());\n        if (args.Contains(\"icons\"))\n            await IconThing.RunAsync();\n        if (args.Contains(\"icon-diff\"))\n            await IconDiff.RunAsync();\n        if (args.Contains(\"brushes\"))\n            await Brushes.GenerateBrushesAsync();\n\n        Console.WriteLine();\n        Console.WriteLine();\n        Console.WriteLine(\"FINISHED\");\n\n        static XElement GetXmlRoot()\n        {\n            var xDocument = XDocument.Load(BaseSnippetLocation);\n            return xDocument.Root ??\n                throw new InvalidDataException(\"The input document does not contain a root\");\n        }\n    }\n\n\n    private static void GenerateClasses(MdPalette palettes)\n    {\n        foreach (var palette in palettes.Palettes ?? Enumerable.Empty<Palette>())\n        {\n            var sb = new StringBuilder();\n\n            var colors = new StringBuilder();\n            var hueNames = new StringBuilder();\n            var shortName = palette.Name?.Replace(\" \", \"\");\n\n            for (var i = 0; i < palette.Hexes?.Length; i++)\n            {\n                var colorName = shortName + palettes.Shades?[i];\n                colors.AppendLine($\"\\tpublic static Color {colorName} {{ get; }} = (Color)ColorConverter.ConvertFromString(\\\"{palette.Hexes[i]}\\\");\");","sourceCodeStart":73,"sourceCodeEnd":109,"githubUrl":"https://github.com/MaterialDesignInXAML/MaterialDesignInXamlToolkit/blob/98edec3a0b96ef272c587b14bcd67ef5f928a7ed/src/MaterialDesignToolkit.ResourceGeneration/Program.cs#L73-L109","documentation":"The local function GetXmlRoot calls XDocument.Load on the snippet file 'MaterialColourSwatchesSnippet.xml' and then demands xDocument.Root be non-null, throwing InvalidDataException otherwise. An XDocument.Root is null only when the parsed document has no single root element — i.e. the file is empty, whitespace-only, or contains only an XML declaration / comments / processing instructions. This input is the scraped Material Design colour-swatch HTML/XML that every generation path (XAML, JSON, named) reads, so a missing root stops all codegen.","triggerScenarios":"The snippet file is missing in a way that XDocument.Load still parses (extremely rare) or, more realistically, the file exists but is empty or truncated so it has no document element. Also fires if the file was replaced by HTML preprocessing output that emitted only a comment or an <?xml ...?> declaration with nothing under it.","commonSituations":"The MaterialColourSwatchesSnippet.xml file was not regenerated/downloaded before running the tool; a merge or git operation left it empty; an editor or formatter stripped its contents; the download script that produces it failed midway and wrote a 0-byte file; the file path is case-sensitive on Linux and the actual file has different casing.","solutions":["Verify the snippet file exists and has content: check File.Exists(BaseSnippetLocation) and that its Length > 0 before calling XDocument.Load.","Open MaterialColourSwatchesSnippet.xml in an editor and confirm it has a single outermost element (e.g. a <div> or <root>) wrapping all <section> nodes.","Re-run whatever step scrapes/produces MaterialColourSwatchesSnippet.xml so the file is regenerated with valid content.","If you are on a case-sensitive filesystem, confirm the filename casing exactly matches the const 'MaterialColourSwatchesSnippet.xml'.","Add a pre-load guard (file-not-found + empty-file checks) so future failures point at the real cause instead of a null-root throw."],"exampleFix":"// before\nvar xDocument = XDocument.Load(BaseSnippetLocation);\nreturn xDocument.Root ??\n    throw new InvalidDataException(\"The input document does not contain a root\");\n\n// after\nif (!File.Exists(BaseSnippetLocation))\n    throw new FileNotFoundException($\"Snippet file not found: {Path.GetFullPath(BaseSnippetLocation)}\", BaseSnippetLocation);\nif (new FileInfo(BaseSnippetLocation).Length == 0)\n    throw new InvalidDataException($\"Snippet file is empty: {Path.GetFullPath(BaseSnippetLocation)}\");\n\nvar xDocument = XDocument.Load(BaseSnippetLocation);\nreturn xDocument.Root ??\n    throw new InvalidDataException($\"The input document '{BaseSnippetLocation}' does not contain a root element\");","handlingStrategy":"validation","validationCode":"const string path = \"MaterialColourSwatchesSnippet.xml\";\nif (!File.Exists(path))\n    throw new FileNotFoundException($\"Snippet file not found: {Path.GetFullPath(path)}\", path);\nif (new FileInfo(path).Length == 0)\n    throw new InvalidDataException($\"Snippet file is empty: {Path.GetFullPath(path)}\");\n\nvar root = XDocument.Load(path).Root\n    ?? throw new InvalidDataException($\"Snippet '{path}' has no root element\");","typeGuard":"static bool HasValidXmlRoot(string path) =>\n    File.Exists(path) &&\n    new FileInfo(path).Length > 0 &&\n    XDocument.Load(path).Root is not null;","tryCatchPattern":"try\n{\n    var root = GetXmlRoot();\n}\ncatch (InvalidDataException ex) when (ex.Message.Contains(\"does not contain a root\"))\n{\n    // treat as a corrupt/missing snippet: log the resolved path and abort codegen cleanly\n    Console.Error.WriteLine($\"Snippet '{Path.GetFullPath(BaseSnippetLocation)}' is corrupt or empty: {ex.Message}\");\n    throw;\n}","preventionTips":["Always regenerate MaterialColourSwatchesSnippet.xml from its source before running the generator.","Add a pre-flight check (File.Exists + non-zero Length) before XDocument.Load.","Commit the snippet file so a fresh clone always has it.","On case-sensitive filesystems, keep the exact filename casing 'MaterialColourSwatchesSnippet.xml'."],"tags":["csharp","xml","codegen","file-io"],"backgroundTag":null,"analyzedSha":"98edec3a0b96ef272c587b14bcd67ef5f928a7ed","analyzedAt":"2026-08-13T14:31:19.111Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}