dotnet/wpf · error · ArgumentNullException

XmlReader is null

Error message

XmlReader is null

What it means

ArgumentNullException thrown by XamlTextReader.Initialize (invoked from the XamlTextReader constructors, e.g. the Clone and reader-creation paths used by Stroke-like consumers) when the given XmlReader is null. The XAML text reader requires a real XmlReader to wrap (optionally with markup-compatibility processing), so null input is rejected immediately with the literal paramName "XmlReader is null".

Solutions

  1. Ensure the XmlReader is created successfully (XmlReader.Create over a valid stream/URI) before passing it to XamlTextReader.
  2. Null-check the reader at the call site and fail with a clear message identifying the missing source file/stream.
  3. If the reader may legitimately be absent, skip constructing XamlTextReader rather than passing null.
  4. Check that any upstream factory (e.g. stream open, file load) returned non-null before chaining into the reader.

Example fix

// before
XmlReader xml = TryOpen(path); // may return null
var reader = new XamlTextReader(xml, schemaContext, settings); // ArgumentNullException
// after
XmlReader xml = TryOpen(path) ?? throw new FileNotFoundException("XAML source not found", path);
var reader = new XamlTextReader(xml, schemaContext, settings);
Defensive patterns

Strategy: validation

Validate before calling

if (xmlReader == null)
    throw new ArgumentException("XmlReader must be created before constructing XamlTextReader.");

Type guard

static bool IsValidReader(XmlReader r) => r is XmlReader { IsDefault: _ } && r is not null; // simplest: r != null

Try / catch

try { var reader = new XamlTextReader(xmlReader, sc, settings); }
catch (ArgumentNullException ex) when (ex.ParamName == "XmlReader is null") { FailWithSourceDiagnostic(); }

Prevention

When it happens

Trigger: Calling a XamlTextReader constructor (or Clone) with a null XmlReader argument — e.g. XmlReader.Create failed silently upstream, a factory method returned null, or a caller passed a disposed/uninitialized reader variable.

Common situations: XmlReader.Create over a missing stream/file path configuration; pipeline stages that pass through a nullable reader without checking; cloning a reader whose underlying XmlReader was already released.

Related errors


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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/InfosetObjects/XamlTextReader.cs:70

            ArgumentNullException.ThrowIfNull(schemaContext);

            Initialize(xmlReader, schemaContext, null);
        }

        public XamlTextReader(XmlReader xmlReader, XamlSchemaContext schemaContext, XamlTextReaderSettings settings)
        {
            ArgumentNullException.ThrowIfNull(schemaContext);

            Initialize(xmlReader, schemaContext, settings);
        }

        private void Initialize(XmlReader givenXmlReader, XamlSchemaContext schemaContext, XamlTextReaderSettings settings)
        {
            XmlReader myXmlReader;

            if (givenXmlReader == null)
            {
                throw new ArgumentNullException("XmlReader is null");
            }

            _mergedSettings = (settings == null) ? new XamlTextReaderSettings() : new XamlTextReaderSettings(settings);

            //Wrap the xmlreader with a XmlCompatReader instance to apply MarkupCompat rules.
            if (!_mergedSettings.SkipXmlCompatibilityProcessing)
            {
                XmlCompatibilityReader mcReader =
                        new XmlCompatibilityReader(givenXmlReader,
                                new IsXmlNamespaceSupportedCallback(IsXmlNamespaceSupported)
                        );
                myXmlReader = mcReader;
            }
            else
            {   // Don't wrap the xmlreader with XmlCompatReader.
                // Useful for uses where users want to keep mc: content in the XamlNode stream.
                // Or have already processed the markup compat and want that extra perf.
                // We need to go make sure the parser thinks it knows mc: uri,

View on GitHub (pinned to 81131a70a4)