iOfficeAI/OfficeCLI · error · ArgumentException
Invalid path index '{indexStr}' in segment '{part}'. Expecte
Error message
Invalid path index '{indexStr}' in segment '{part}'. Expected a numeric index. What it means
Thrown by the GenericXmlQuery path parser when the bracket content is neither a numeric index nor a recognized predicate. After extracting the text between '[' and ']', int.TryParse fails and IsContentFilterPath returns false, so the parser cannot interpret the segment as a position or a filter.
Source
Thrown at src/officecli/Core/GenericXmlQuery.cs:261
var bracketIdx = part.IndexOf('[');
if (bracketIdx >= 0)
{
// BUG-R36-01 fix: when ']' is missing (e.g. "slide[") the expression
// part[(bracketIdx+1)..^1] produces a negative-length range crash.
// Detect and reject unclosed brackets with a clean ArgumentException.
var closingIdx = part.IndexOf(']', bracketIdx + 1);
if (closingIdx < 0)
throw new ArgumentException($"Malformed path segment '{part}'. Bracket '[' is not closed. Expected format: name[index] or name[@attr=value].");
var name = PathAliases.Resolve(part[..bracketIdx]);
var indexStr = part[(bracketIdx + 1)..^1];
if (!int.TryParse(indexStr, out var idx))
// A predicate in the index slot (row[Score>0], row[not(V)])
// means the caller reached the single-node path navigator
// with a FILTER. get is one-node-by-path by contract;
// point at the verbs that run the selector engine.
throw new ArgumentException(AttributeFilter.IsContentFilterPath($"[{indexStr}]")
? $"'{part}' is a predicate, but this verb navigates by position and expects a numeric index (e.g. {part[..part.IndexOf('[')]}[2]). Predicates work on 'query' (read) and 'set'/'remove' (mutate matched elements)."
: $"Invalid path index '{indexStr}' in segment '{part}'. Expected a numeric index.");
if (idx < 1)
throw new ArgumentException($"Invalid path index '{idx}' in segment '{part}'. Index must be >= 1.");
segments.Add((name, idx));
}
else
{
segments.Add((PathAliases.Resolve(part), null));
}
}
return segments;
}
/// <summary>
/// Navigate an OpenXML element tree by path segments (localName + optional 1-based index).
/// Returns null if any segment cannot be resolved.
/// </summary>
public static OpenXmlElement? NavigateByPath(OpenXmlElement root, IReadOnlyList<(string Name, int? Index)> segments)
{View on GitHub (pinned to 1ced45e900)
Solutions
- Use a 1-based numeric index (e.g. row[2], not row[1.5] or row[abc]).
- If you meant a predicate, use recognized predicate syntax (e.g. @attr=value or a content filter) on the query/set/remove verbs.
- Check for typos or placeholder text inside the brackets.
Example fix
// before Get(xml, "rows/row[two]"); // not numeric, not a predicate // after Get(xml, "rows/row[2]");
Defensive patterns
Strategy: validation
Validate before calling
static bool IsIndexSegment(string segment)
{
var b = segment.IndexOf('[');
if (b < 0) return true;
var inner = segment[(b + 1)..^1];
return int.TryParse(inner, out _);
} Try / catch
try { Get(xml, path); }
catch (ArgumentException ex) when (ex.Message.Contains("Expected a numeric index"))
{ /* convert to numeric index or switch verb */ } Prevention
- Use 1-based numeric indices for positional navigation.
- Route predicate-style filters to the query verb.
When it happens
Trigger: Calling a path navigator with a segment like 'row[abc]' or 'slide[1.5]' — non-numeric, non-predicate bracket content that fails both int.TryParse and the predicate check.
Common situations: Typo in an index; attempting zero-based indexing (the parser expects 1-based); a malformed predicate not recognized by the filter detector; leftover placeholder text.
Related errors
- Malformed path segment '{part}'. Bracket '[' is not closed.
- Invalid path index '{idx}' in segment '{part}'. Index must b
- '{part}' is a predicate, but this verb navigates by position
- Unknown action: {action}. Supported: append, prepend, insert
- raw-set: XPath matched no elements: {xpath}. Hint: auto-regi
AI-assisted analysis of iOfficeAI/OfficeCLI@1ced45e900 (2026-08-13).
Data as JSON: /api/errors/e922f828be86e3ed.
Report an issue: GitHub.