dotnet/wpf · error

ArgumentNullException(nameof(xmlParserContext))

Error message

ArgumentNullException(nameof(xmlParserContext))

What it means

The ParserContext(XmlParserContext) constructor requires a non-null xmlParserContext; passing null throws ArgumentNullException(nameof(xmlParserContext)). The constructor copies XmlLang, XmlSpace and other settings out of the given XmlParserContext, so it cannot proceed without one.

Solutions

  1. Ensure the XmlParserContext is created (new XmlParserContext(...)) before wrapping it in ParserContext
  2. Add a null check or ArgumentNullException.ThrowIfNull on the value before calling the constructor
  3. Use the parameterless ParserContext() constructor if no XML context is available
  4. Fix upstream code that returns null XmlParserContext on failure

Example fix

// before
ParserContext pc = new ParserContext(GetXmlParserContext()); // may return null
// after
XmlParserContext xmlCtx = GetXmlParserContext() ?? new XmlParserContext(null, null, null, XmlSpace.None);
ParserContext pc = new ParserContext(xmlCtx);
Defensive patterns

Strategy: validation

Validate before calling

ArgumentNullException.ThrowIfNull(xmlParserContext);
var pc = new ParserContext(xmlParserContext);

Type guard

ParserContext? TryCreateParserContext(XmlParserContext? ctx) => ctx is null ? null : new ParserContext(ctx);

Try / catch

try
{
    var pc = new ParserContext(xmlParserContext);
}
catch (ArgumentNullException ex) when (ex.ParamName == "xmlParserContext")
{
    // construct a default ParserContext or abort with a clearer error
}

Prevention

When it happens

Trigger: new ParserContext((XmlParserContext)null) or a variable holding a null XmlParserContext passed to the constructor, typically when a preceding XML load failed and a null context propagated.

Common situations: Wrapping an XmlParserContext produced by System.Xml APIs that may return null, refactoring where context creation was skipped on an error path, or calling the constructor before a XAML reader was configured.

Related errors


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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Markup/ParserContext.cs:93

            _nameScopeStack    = null;
#endif
            _xmlLang        = String.Empty;
            _xmlSpace       = String.Empty;
        }


#if !PBTCOMPILER
        /// <summary>
        ///    Constructor that takes the XmlParserContext.
        ///    A parserContext object will be built based on this.
        /// </summary>
        /// <param name="xmlParserContext">xmlParserContext to use</param>

        public ParserContext(XmlParserContext xmlParserContext)
        {
            if (xmlParserContext == null)
            {
                throw new ArgumentNullException( nameof(xmlParserContext));
            }

            _xmlLang     = xmlParserContext.XmlLang;

            TypeConverter typeConverter = TypeDescriptor.GetConverter(typeof(XmlSpace));
            if (typeConverter != null)
                _xmlSpace = (string) typeConverter.ConvertToString(null, TypeConverterHelper.InvariantEnglishUS, xmlParserContext.XmlSpace);
            else
                _xmlSpace = String.Empty;

            _xmlnsDictionary = new XmlnsDictionary() ;

            if (xmlParserContext.BaseURI != null && xmlParserContext.BaseURI.Length > 0)
            {
                _baseUri = new Uri(xmlParserContext.BaseURI, UriKind.RelativeOrAbsolute);
            }

            XmlNamespaceManager xmlnsManager = xmlParserContext.NamespaceManager;

View on GitHub (pinned to 81131a70a4)