{"record":{"id":"e7f6d292c46b06db","repo":"dotnet/wpf","slug":"sr-unexpectedattribute","errorCode":null,"errorMessage":"SR.UnexpectedAttribute","messagePattern":"SR\\.UnexpectedAttribute","errorType":"exception","errorClass":"XmlException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Annotations/Annotation.cs","lineNumber":550,"sourceCode":"\n        /// <summary>\n        /// Checks all attributes for the current node.  If any attribute isn't a\n        /// namespace attribute an exception is thrown.\n        /// </summary>\n        internal static void CheckForNonNamespaceAttribute(XmlReader reader, string elementName)\n        {\n            Invariant.Assert(reader != null, \"No reader supplied.\");\n            Invariant.Assert(elementName != null, \"No element name supplied.\");\n\n            while (reader.MoveToNextAttribute())\n            {\n                // If the attribute is a namespace declaration we should ignore it\n                if (Annotation.IsNamespaceDeclaration(reader))\n                {\n                    continue;\n                }\n\n                throw new XmlException(SR.Format(SR.UnexpectedAttribute, reader.LocalName, elementName));\n            }\n\n            // We need to move the reader back to the original element the\n            // attributes are on for the next reader operation.  Has no effect\n            // if no attributes were looked at\n            reader.MoveToContent();\n        }\n\n        #endregion Internal Methods\n\n        //------------------------------------------------------\n        //\n        //  Private Properties\n        //\n        //------------------------------------------------------\n\n        #region Private Properties\n","sourceCodeStart":532,"sourceCodeEnd":568,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Annotations/Annotation.cs#L532-L568","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Remove any non-schema, non-xmlns attributes from annotation elements before loading","If extra metadata is needed, wrap the annotation XML in an outer element and keep attributes there","Regenerate the XML with Annotation.WriteXml to guarantee only schema attributes are present"],"exampleFix":"// before\n<Annotation Id=\"...\" Creator=\"...\" Type=\"...\" trackingId=\"123\">\n// after\n<Annotation Id=\"...\" Creator=\"...\" Type=\"...\">","handlingStrategy":"validation","validationCode":"XDocument doc = XDocument.Load(annotationStream);\nvar badAttrs = doc.Descendants(\"Annotation\")\n    .SelectMany(e => e.Attributes())\n    .Where(a => !a.IsNamespaceDeclaration\n                && a.Name.LocalName != \"Id\" && a.Name.LocalName != \"Creator\"\n                && a.Name.LocalName != \"Type\")\n    .ToList();\nif (badAttrs.Any()) throw new InvalidOperationException(\"Unexpected attributes on Annotation: \" + string.Join(\",\", badAttrs.Select(a => a.Name.LocalName)));","typeGuard":null,"tryCatchPattern":"try\n{\n    annotation.ReadXml(reader);\n}\ncatch (XmlException ex)\n{\n    logger.LogError(ex, \"Unexpected attribute in annotation XML\");\n    throw new InvalidDataException(\"Annotation element carries unsupported attributes\", ex);\n}","preventionTips":["Never decorate annotation XML elements with custom attributes; use a wrapper element for metadata","Re-save stores through Annotation.WriteXml to normalize attributes","Inspect XML produced by transforms/imports for stray attributes before loading"],"tags":["wpf","annotations","xml","attributes"],"backgroundTag":"schema-validation-failed","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"}