{"record":{"id":"406c24e85b5bcbbe","repo":"dotnet/wpf","slug":"sr-parser-unexpectedtoken","errorCode":null,"errorMessage":"SR.Parser_UnexpectedToken","messagePattern":"SR\\.Parser_UnexpectedToken","errorType":"exception","errorClass":"FormatException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/ParsersCommon.cs","lineNumber":195,"sourceCode":"        private string          _pathString;        // Input string to be parsed\n        private int             _pathLength;\n        private int             _curIndex;          // Location to read next character from \n        private bool            _figureStarted;     // StartFigure is effective\n        \n        private Point           _lastStart;         // Last figure starting point\n        private Point           _lastPoint;         // Last point\n        private Point           _secondLastPoint;   // The point before last point\n        \n        private char            _token;             // Non whitespace character returned by ReadToken\n\n        private StreamGeometryContext _context;\n        \n        /// <summary>\n        /// Throw unexpected token exception\n        /// </summary>\n        private void ThrowBadToken()\n        {\n            throw new System.FormatException(SR.Format(SR.Parser_UnexpectedToken, _pathString, _curIndex - 1));\n        }\n\n        private bool More()\n        {\n            return _curIndex < _pathLength;\n        }\n        \n        // Skip white space, one comma if allowed\n        private bool SkipWhiteSpace(bool allowComma)\n        {\n            bool commaMet = false;\n            \n            while (More())\n            {\n                char ch = _pathString[_curIndex];\n                \n                switch (ch)\n                {","sourceCodeStart":177,"sourceCodeEnd":213,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/ParsersCommon.cs#L177-L213","documentation":"ThrowBadToken is the shared failure routine of the path mini-language tokenizer (the class wrapping the geometry string). Whenever SkipWhiteSpace, IsNumber, ReadNumber, ReadBool, or ParseToGeometryContext encounters a character that cannot start a valid token in the current position, it calls ThrowBadToken, which throws FormatException with SR.Parser_UnexpectedToken including the offending path string and the character index.","triggerScenarios":"StreamGeometry.Parse / Geometry.Parse / Path.Data with a character where a command letter or number is expected — e.g. 'M0,0;L10,10' (semicolon), 'M 0,0 X 5,5' (unknown command X), or a stray letter/number in an unexpected slot like 'M0,,0'.","commonSituations":"Typos in hand-written XAML Data attributes; pasted SVG data containing unsupported commands (arcs with flags in different order is fine, but e.g. lowercase-disallowed contexts or 'H/V' misuse handled elsewhere); generated strings with separators WPF does not accept.","solutions":["Inspect the exception message: it names the path string and the offending index; fix the character at that position.","Use only WPF mini-language commands: M/m, L/l, H/h, V/v, C/c, Q/q, S/s, T/t, A/a, Z/z, with F0/F1 optionally at the start.","Remove characters not in the mini-language (semicolons, parentheses, unit suffixes like 'px').","Wrap parsing in try-catch for FormatException and fall back to a safe geometry."],"exampleFix":"// before\ngeo = StreamGeometry.Parse(\"M 0,0 X 5,5\"); // 'X' is not a command -> throws\n\n// after\ngeo = StreamGeometry.Parse(\"M 0,0 L 5,5\"); // valid line command","handlingStrategy":"try-catch","validationCode":"static readonly Regex AllowedChars = new Regex(\"^[MmLlHhVvCcQqSsTtAaZzFf0-9,.eE+\\-\\s]*$\");\nstatic bool HasOnlyMiniLanguageChars(string path) => path != null && AllowedChars.IsMatch(path);","typeGuard":null,"tryCatchPattern":"try { return StreamGeometry.Parse(data); }\ncatch (FormatException ex)\n{\n    // message contains the offending path and index\n    log.Warn(ex, \"Unexpected token in path data\");\n    return Geometry.Empty;\n}","preventionTips":["Read the exception message's index to pinpoint the bad character.","Sanitize pasted SVG data before assigning to Path.Data.","Prefer building geometries with StreamGeometryContext for programmatic shapes."],"tags":["wpf","path-geometry","parser","format-exception"],"backgroundTag":"invalid-argument-format","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-21T21:30:21.729Z"}