AvaloniaUI/Avalonia · error · InvalidDataException

Unexpected path command '{c}'.

Error message

Unexpected path command '{c}'.

What it means

Thrown by ReadCommand when the next non-whitespace character is not a recognized path command letter. The s_commands table accepts only F, M, L, H, V, Q, T, C, S, A, Z (case-insensitive — lower-case means relative). Any other character as a command position triggers this.

Source

Thrown at src/Avalonia.Base/Media/PathMarkupParser.cs:603

            var x = ReadDouble(ref span);
            span = ReadSeparator(span);
            var y = ReadDouble(ref span);
            return new Point(origin.X + x, origin.Y + y);
        }

        private static bool ReadCommand(ref ReadOnlySpan<char> span, out Command command, out bool relative)
        {
            span = SkipWhitespace(span);
            if (span.IsEmpty)
            {
                command = default;
                relative = false;
                return false;
            }
            var c = span[0];
            if (!s_commands.TryGetValue(char.ToUpperInvariant(c), out command))
            {
                throw new InvalidDataException("Unexpected path command '" + c + "'.");
            }
            relative = char.IsLower(c);
            span = span.Slice(1);
            return true;
        }

        [MemberNotNull(nameof(_geometryContext))]
        private void ThrowIfDisposed()
        {
            if (_isDisposed || _geometryContext is null)
                throw new ObjectDisposedException(nameof(PathMarkupParser));
        }
    }
}

View on GitHub (pinned to 11c5427268)

Solutions

  1. Use only the supported command letters (F M L H V Q T C S A Z); lower-case variants are relative.
  2. Check the character reported in the message — it pinpoints the offending token.
  3. Strip non-path text (XML attributes, namespaces) before passing the string to Parse().

Example fix

// before
geometry.Parse("M0,0 X10,10");

// after
geometry.Parse("M0,0 L10,10");
Defensive patterns

Strategy: validation

Validate before calling

static readonly HashSet<char> ValidCommands = new("FMLHVQTCSAZ");
static bool IsKnownCommand(char c) => ValidCommands.Contains(char.ToUpperInvariant(c));

Type guard

static bool IsKnownCommand(char c) => "FMLHVQTCSAZ".Contains(char.ToUpperInvariant(c));

Try / catch

try { parser.Parse(data); }
catch (InvalidDataException ex) when (ex.Message.StartsWith("Unexpected path command"))
{ /* report the offending character from the message */ }

Prevention

When it happens

Trigger: Path strings with a stray character where a command letter is expected, e.g. 'M0,0 X10,10' (X is not a command), 'M0,0 l10 10b5,5' (b is not a command), or a leading punctuation mark.

Common situations: Typos in hand-written path data; mixing in SVG elements/attributes instead of the path 'd' grammar; stray text concatenated onto the data string; case where a number runs into where a command was expected after a malformed token.

Related errors


AI-assisted analysis of AvaloniaUI/Avalonia@11c5427268 (2026-08-13). Data as JSON: /api/errors/c37b5505c22c68ee. Report an issue: GitHub.