dotnet/wpf · error · ApplicationException

Could not load data file.

Error message

Could not load data file.

What it means

LoadDocument in ResourceGenerator calls XmlLoader.Load(xmlFile, schemaFile) and throws ApplicationException('Could not load data file.') when the loader returns null. A null result means the XML document could not be loaded/validated against the XSD schema (the loader swallows the underlying failure and reports it by returning null).

Solutions

  1. Validate the XML against the XSD manually (e.g. with xmllint --schema or Visual Studio) to see the concrete validation error.
  2. Fix XML well-formedness errors (unclosed tags, invalid characters, wrong encoding declaration).
  3. Update the XML to conform to the current schema, or use the matching schema version.
  4. Check that the file is readable and not empty/locked by another process.

Example fix

// validate before invoking the generator
xmllint --noout --schema schema.xsd model.xml
// fix reported validation errors, then
csp ResourceGenerator model.xml schema.xsd
Defensive patterns

Strategy: validation

Validate before calling

// validate XML against the XSD before invoking the generator
using var reader = XmlReader.Create(xmlPath);
var doc = new XmlDocument();
doc.Load(reader); // throws with the concrete parse error
var schemas = new XmlSchemaSet();
schemas.Add(null, schemaPath);
doc.Schemas = schemas;
doc.Validate(null); // throws with the concrete validation error

Try / catch

try { generator.LoadDocument(xmlFile, schemaFile); }
catch (ApplicationException ex) when (ex.Message.Contains("Could not load data file")) { /* run xmllint --schema to get the real underlying error */ }

Prevention

When it happens

Trigger: XmlLoader.Load returning null because the XML is malformed, fails XSD schema validation, or cannot be read, after both input files passed the existence check in ProcessArgs.

Common situations: Hand-edited XML with a syntax error; XML that does not conform to the schema (missing required elements/attributes, wrong element order); encoding problems; a schema/XML version mismatch after edits.

Related errors


AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14). Data as JSON: /api/errors/45d4505a9959966f. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/WpfGfx/codegen/mcg/main/ResourceGenerator.cs:221

        {
            Usage();
            throw new ApplicationException(message);
        }


        //+-----------------------------------------------------------------------------
        //
        //  Function:  LoadDocument
        //
        //------------------------------------------------------------------------------

        private static XmlDocument LoadDocument(string xmlFile, string schemaFile)
        {
            XmlDocument document = XmlLoader.Load(xmlFile, schemaFile);

            if (document == null)
            {
                throw new ApplicationException(String.Format("Could not load data file."));
            }

            return document;
        }


        //+-----------------------------------------------------------------------------
        //
        //  Function:  DisplayLogo
        //
        //------------------------------------------------------------------------------

        private static void DisplayLogo()
        {
            Console.WriteLine();
            Console.WriteLine("Avalon MilCodeGen source-code generator.");
            Console.WriteLine();
        }

View on GitHub (pinned to 81131a70a4)