dotnet/wpf · error · XmlException

SR.UnexpectedAttribute

Error message

SR.UnexpectedAttribute

What it means

CheckForNonNamespaceAttribute (called from ReadXml) throws XmlException with SR.UnexpectedAttribute when the Annotation element carries an attribute that is neither a recognized schema attribute nor a namespace declaration. The parser only tolerates schema attributes and xmlns declarations; anything else is a hard failure.

Solutions

  1. Remove any non-schema, non-xmlns attributes from annotation elements before loading
  2. If extra metadata is needed, wrap the annotation XML in an outer element and keep attributes there
  3. Regenerate the XML with Annotation.WriteXml to guarantee only schema attributes are present

Example fix

// before
<Annotation Id="..." Creator="..." Type="..." trackingId="123">
// after
<Annotation Id="..." Creator="..." Type="...">
Defensive patterns

Strategy: validation

Validate before calling

XDocument doc = XDocument.Load(annotationStream);
var badAttrs = doc.Descendants("Annotation")
    .SelectMany(e => e.Attributes())
    .Where(a => !a.IsNamespaceDeclaration
                && a.Name.LocalName != "Id" && a.Name.LocalName != "Creator"
                && a.Name.LocalName != "Type")
    .ToList();
if (badAttrs.Any()) throw new InvalidOperationException("Unexpected attributes on Annotation: " + string.Join(",", badAttrs.Select(a => a.Name.LocalName)));

Try / catch

try
{
    annotation.ReadXml(reader);
}
catch (XmlException ex)
{
    logger.LogError(ex, "Unexpected attribute in annotation XML");
    throw new InvalidDataException("Annotation element carries unsupported attributes", ex);
}

Prevention

When it happens

Trigger: Loading annotation XML whose <Annotation> (or nested) element has extra attributes, such as custom metadata attributes, xsi:* misuse, or attributes added by an upstream tool.

Common situations: Annotating the stored XML with tracking attributes (e.g. id refs from a CMS); transforms that add attributes; XSLT or DOM edits that introduce attributes on annotation elements.

Understand the failure class

Background: Schema validation failed / invalid input schema: payload rejected because its shape doesn't match the expected schema — this error's family across 28 libraries.

Related errors


AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14). Data as JSON: /api/errors/e7f6d292c46b06db. Report an issue: GitHub.

Appendix: source

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

        /// <summary>
        /// Checks all attributes for the current node.  If any attribute isn't a
        /// namespace attribute an exception is thrown.
        /// </summary>
        internal static void CheckForNonNamespaceAttribute(XmlReader reader, string elementName)
        {
            Invariant.Assert(reader != null, "No reader supplied.");
            Invariant.Assert(elementName != null, "No element name supplied.");

            while (reader.MoveToNextAttribute())
            {
                // If the attribute is a namespace declaration we should ignore it
                if (Annotation.IsNamespaceDeclaration(reader))
                {
                    continue;
                }

                throw new XmlException(SR.Format(SR.UnexpectedAttribute, reader.LocalName, elementName));
            }

            // We need to move the reader back to the original element the
            // attributes are on for the next reader operation.  Has no effect
            // if no attributes were looked at
            reader.MoveToContent();
        }

        #endregion Internal Methods

        //------------------------------------------------------
        //
        //  Private Properties
        //
        //------------------------------------------------------

        #region Private Properties

View on GitHub (pinned to 81131a70a4)