dotnet/wpf · error

ArgumentNullException(nameof(parserContext))

Error message

ArgumentNullException(nameof(parserContext))

What it means

The static ParserContext.ToXmlParserContext(ParserContext) converter requires a non-null parserContext and throws ArgumentNullException(nameof(parserContext)) when given null. It translates a XAML ParserContext into a System.Xml XmlParserContext (namespace manager, XmlLang, XmlSpace), which is impossible without a source context.

Solutions

  1. Pass a valid ParserContext instance (use new ParserContext() if defaults suffice)
  2. Null-check the context before calling ToXmlParserContext
  3. Fix the producer of the ParserContext so it never returns null
  4. Wrap in ArgumentNullException.ThrowIfNull(parserContext) guard at the call site

Example fix

// before
ParserContext pc = CreateParserContext(); // may be null
XmlParserContext xmlCtx = ParserContext.ToXmlParserContext(pc);
// after
ParserContext pc = CreateParserContext() ?? new ParserContext();
XmlParserContext xmlCtx = ParserContext.ToXmlParserContext(pc);
Defensive patterns

Strategy: validation

Validate before calling

ArgumentNullException.ThrowIfNull(parserContext);
var xmlCtx = ParserContext.ToXmlParserContext(parserContext);

Type guard

XmlParserContext? TryToXmlParserContext(ParserContext? ctx) => ctx is null ? null : ParserContext.ToXmlParserContext(ctx);

Try / catch

try
{
    var xmlCtx = ParserContext.ToXmlParserContext(parserContext);
}
catch (ArgumentNullException ex) when (ex.ParamName == "parserContext")
{
    xmlCtx = ParserContext.ToXmlParserContext(new ParserContext());
}

Prevention

When it happens

Trigger: Calling ParserContext.ToXmlParserContext(null), or passing a ParserContext variable that was never initialized or was set to null after a failed load, commonly in custom XAML reader/pipeline code.

Common situations: Custom XamlReader pipelines converting contexts between System.Xaml and System.Xml, error paths where a ParserContext factory returned null, and unit tests exercising edge cases.

Related errors


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

Appendix: source

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

        /// </summary>
        /// <param name="parserContext">ParserContext to Convert</param>
        /// <returns>XmlParserContext</returns>
        public static implicit operator XmlParserContext(ParserContext parserContext)
        {
            return ParserContext.ToXmlParserContext(parserContext);
        }


        /// <summary>
        ///    Operator for Converting a ParserContext to an XmlParserContext
        /// </summary>
        /// <param name="parserContext">ParserContext to Convert</param>
        /// <returns>XmlParserContext</returns>
        public static XmlParserContext ToXmlParserContext(ParserContext parserContext)
        {
            if (parserContext == null)
            {
                throw new ArgumentNullException( nameof(parserContext));
            }

            XmlNamespaceManager xmlnsMgr = new XmlNamespaceManager(new NameTable());
            XmlSpace xmlSpace = System.Xml.XmlSpace.None;

            if (parserContext.XmlSpace != null && parserContext.XmlSpace.Length != 0)
            {
                TypeConverter typeConverter = TypeDescriptor.GetConverter(typeof(System.Xml.XmlSpace));
                if (null != typeConverter)
                {
                    try
                    {
                        xmlSpace = (System.Xml.XmlSpace)typeConverter.ConvertFromString(null, TypeConverterHelper.InvariantEnglishUS, parserContext.XmlSpace);
                    }
                    catch (System.FormatException)  // If it's not a valid space value, ignore it
                    {
                        xmlSpace = System.Xml.XmlSpace.None;
                    }

View on GitHub (pinned to 81131a70a4)