dotnet/wpf · error · ArgumentException

SR.Format(SR.XmlLangMalformed, ietfLanguageTag)

Error message

SR.Format(SR.XmlLangMalformed, ietfLanguageTag)

What it means

XmlLanguage.Parse / GetLanguage received an IETF language tag that does not conform to BCP-47 subtag syntax (e.g. empty, illegal or non-7-bit-ASCII characters, malformed subtags). ThrowParseException wraps the offending tag in an ArgumentException.

Solutions

  1. Validate the tag matches BCP-47 syntax (letters/digits separated by single hyphens) before calling GetLanguage
  2. Source tags from CultureInfo.IetfLanguageTag or Name instead of display names
  3. Normalize case and strip whitespace; catch ArgumentException for tolerant parsing of user input

Example fix

// before
var lang = XmlLanguage.GetLanguage(userInput); // throws on 'de(1)'
// after
if (System.Text.RegularExpressions.Regex.IsMatch(userInput, "^[a-zA-Z0-9]+(-[a-zA-Z0-9]+)*$"))
{ var lang = XmlLanguage.GetLanguage(userInput.ToLowerInvariant()); }
Defensive patterns

Strategy: validation

Validate before calling

static bool IsValidIetfTag(string tag) =>
    !string.IsNullOrWhiteSpace(tag) &&
    System.Text.RegularExpressions.Regex.IsMatch(tag, "^[a-zA-Z]{1,8}(-[a-zA-Z0-9]{1,8})*$");

Prevention

When it happens

Trigger: XmlLanguage.GetLanguage(tag) or Parse with tags containing non-ASCII characters, invalid characters in subtags, leading/trailing hyphens, or empty strings.

Common situations: Passing user-entered or OS-supplied locale strings; using display names ('English (US)') instead of tags ('en-us'); non-7-bit-ASCII characters from copy-paste.

Understand the failure class

Background: "Invalid ... format", "must be in format X", "does not look like a ..." — invalid argument format errors across CLI tools and libraries — this error's family across 17 libraries.

Related errors


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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Markup/XmlLanguage.cs:792

                        ThrowParseException(ietfLanguageTag);
                    }
                }
            }
        }

        private static bool IsLowerAlpha(int c)
        {
            return (c >= 'a' && c <= 'z');
        }

        private static bool IsDigit(int c)
        {
            return c >= '0' && c <= '9';
        }

        private static void ThrowParseException(string ietfLanguageTag)
        {
             throw new ArgumentException(SR.Format(SR.XmlLangMalformed, ietfLanguageTag), nameof(ietfLanguageTag));
        }

        // throws if there is a non-7-bit ascii character
        private static string AsciiToLower(string tag)
        {
            int length = tag.Length;

            for (int i = 0; i < length; i++)
            {
                if (tag[i] > 127)
                {
                    ThrowParseException(tag);
                }
            }

            return tag.ToLowerInvariant();
        }
}

View on GitHub (pinned to 81131a70a4)