dotnet/wpf · error · FormatException

SR.Format(SR.InvalidAttributeValue…

Error message

SR.Format(SR.InvalidAttributeValue, AnnotationXmlConstants.Attributes.TypeName)

What it means

ReadAttributes throws FormatException with SR.Format(SR.InvalidAttributeValue, TypeName) when the value of the Id or TypeName attribute is just a string of whitespace (empty after splitting on ':'). The TypeName attribute must contain a non-empty name, optionally qualified by a namespace prefix.

Solutions

  1. Ensure the Type attribute contains a valid, non-empty type name before loading
  2. Trim and validate attribute values in the XML-generation pipeline
  3. Catch FormatException in the load path and report which attribute was invalid

Example fix

// before
<Annotation Id="..." Type=" ">
// after
<Annotation Id="..." Type="Note">
Defensive patterns

Strategy: validation

Validate before calling

string typeName = (string)(XDocument.Load(stream).Root?.Attribute("Type")) ?? "";
if (string.IsNullOrWhiteSpace(typeName))
    throw new InvalidDataException("Type attribute is missing or whitespace");

Type guard

bool IsValidTypeNameAttribute(string value) => !string.IsNullOrWhiteSpace(value) && !value.StartsWith(":") && !value.EndsWith(":") && value.Count(c => c == ':') <= 1;

Try / catch

try
{
    annotation.ReadXml(reader);
}
catch (FormatException ex)
{
    logger.LogError(ex, "Annotation Type attribute value is invalid");
    throw new InvalidDataException("Invalid Type attribute in annotation XML", ex);
}

Prevention

When it happens

Trigger: Loading annotation XML where the Type attribute is whitespace only (e.g. Type=" "), producing a single segment whose name span is empty after trim.

Common situations: XML templating that leaves an empty attribute placeholder; XSLT transforms emitting Type=" "; hand-edited annotation stores.

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/0e9cc4517df98bbf. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Annotations/Annotation.cs:640

                        break;

                    case AnnotationXmlConstants.Attributes.LastModificationTime:
                        _modified = XmlConvert.ToDateTime(value);
                        break;

                    #pragma warning restore 0618

                    case AnnotationXmlConstants.Attributes.TypeName:
                        ReadOnlySpan<char> typeName = value.AsSpan();
                        int segmentsLength = typeName.Split(segments, Colon, StringSplitOptions.TrimEntries);

                        if (segmentsLength == 1) // Contains only name
                        {
                            ReadOnlySpan<char> name = typeName[segments[0]];
                            if (name.IsEmpty)
                            {
                                // Just a string of whitespace (empty string doesn't get processed)
                                throw new FormatException(SR.Format(SR.InvalidAttributeValue, AnnotationXmlConstants.Attributes.TypeName));
                            }
                            _typeName = new XmlQualifiedName(name.ToString());
                        }
                        else if (segmentsLength == 2) //Contains both namespace:name
                        {
                            ReadOnlySpan<char> @namespace = typeName[segments[0]];
                            ReadOnlySpan<char> name = typeName[segments[1]];
                            if (@namespace.IsEmpty || name.IsEmpty)
                            {
                                // One colon, prefix or suffix is empty string or whitespace
                                throw new FormatException(SR.Format(SR.InvalidAttributeValue, AnnotationXmlConstants.Attributes.TypeName));
                            }
                            _typeName = new XmlQualifiedName(name.ToString(), reader.LookupNamespace(@namespace.ToString()));
                        }
                        else
                        {
                            // More than one colon
                            throw new FormatException(SR.Format(SR.InvalidAttributeValue, AnnotationXmlConstants.Attributes.TypeName));

View on GitHub (pinned to 81131a70a4)