iOfficeAI/OfficeCLI · error · ArgumentException

setattr format: name=value

Error message

setattr format: name=value

What it means

Thrown during setattr when the xml value (the name=value string) either contains no '=' character at all (IndexOf returns -1) or has '=' as the very first character (IndexOf returns 0, meaning the attribute name is empty). The format requires a non-empty name before the '=' delimiter.

Source

Thrown at src/officecli/Core/RawXmlHelper.cs:234

                    break;

                case "replace":
                    if (xml == null) throw new ArgumentException("--xml is required for replace");
                    var replaceFragment = ParseFragment(xml, xDoc);
                    node.ReplaceWith(replaceFragment.ToArray());
                    affected++;
                    break;

                case "remove" or "delete":
                    RequireParent(node, "remove");
                    node.Remove();
                    affected++;
                    break;

                case "setattr":
                    if (xml == null) throw new ArgumentException("--xml is required for setattr (format: name=value)");
                    var eqIdx = xml.IndexOf('=');
                    if (eqIdx <= 0) throw new ArgumentException("setattr format: name=value");
                    var attrName = xml[..eqIdx];
                    var attrValue = xml[(eqIdx + 1)..];

                    // Handle namespaced attributes (e.g. w:val)
                    var colonIdx = attrName.IndexOf(':');
                    if (colonIdx > 0)
                    {
                        var prefix = attrName[..colonIdx];
                        var localName = attrName[(colonIdx + 1)..];
                        var ns = nsManager.LookupNamespace(prefix);
                        if (ns != null)
                            node.SetAttributeValue(XName.Get(localName, ns), attrValue);
                        else
                            node.SetAttributeValue(attrName, attrValue);
                    }
                    else
                    {
                        node.SetAttributeValue(attrName, attrValue);

View on GitHub (pinned to 1ced45e900)

Solutions

  1. Format the value as 'attributename=value', with the name before '=' and the value after.
  2. For namespaced attributes, use 'prefix:localname=value', e.g. 'w:val=center'.
  3. Check for typos: missing '=', wrong delimiter, or reversed order (value=name instead of name=value).

Example fix

// before: missing '=' or wrong format
RawXmlHelper.Execute(root, xpath, "setattr", "w:valcenter");
RawXmlHelper.Execute(root, xpath, "setattr", "center");
RawXmlHelper.Execute(root, xpath, "setattr", "=center");

// after: name=value format
RawXmlHelper.Execute(root, xpath, "setattr", "w:val=center");
Defensive patterns

Strategy: validation

Validate before calling

// Validate name=value format before calling setattr
if (action.Equals("setattr", StringComparison.OrdinalIgnoreCase))
{
    var eqIdx = xml?.IndexOf('=') ?? -1;
    if (eqIdx <= 0)
        throw new ArgumentException($"setattr requires 'name=value' format. Got: '{xml}'");
}

RawXmlHelper.Execute(rootElement, xpath, action, xml);

Try / catch

try
{
    RawXmlHelper.Execute(rootElement, xpath, "setattr", xml);
}
catch (ArgumentException ex) when (ex.Message.Contains("setattr format"))
{
    Console.Error.WriteLine($"setattr requires 'name=value'. Got: '{xml}'");
}

Prevention

When it happens

Trigger: RawXmlHelper.Execute(rootElement, xpath, "setattr", xml) where xml.IndexOf('=') <= 0. Cases: xml has no '=' (e.g. 'w:valcenter'), or xml starts with '=' (e.g. '=center'). The check is eqIdx <= 0, catching both -1 and 0.

Common situations: Caller provides just the attribute name without '=value'. Caller uses a different delimiter (':', ' ', ':=' instead of '='). Caller passes the value first ('=center' or 'center=w:val' where the intent was 'w:val=center'). The xml string is empty or whitespace.

Related errors


AI-assisted analysis of iOfficeAI/OfficeCLI@1ced45e900 (2026-08-13). Data as JSON: /api/errors/37af6fb574472491. Report an issue: GitHub.