{"record":{"id":"45078a1b58a762aa","repo":"dotnet/wpf","slug":"sr-format-sr-xmlnodealreadyowned-change-change","errorCode":null,"errorMessage":"SR.Format(SR.XmlNodeAlreadyOwned, \"change\", \"change\")","messagePattern":"SR\\.Format\\(SR\\.XmlNodeAlreadyOwned, \"change\", \"change\"\\)","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/Annotations/XmlElementCollection.cs","lineNumber":93,"sourceCode":"        /// </summary>\n        protected override void RemoveItem(int index)\n        {\n            XmlElement removedItem = this[index];\n\n            UnregisterForElement(removedItem);\n\n            base.RemoveItem(index);\n        }\n\n        /// <summary>\n        /// called by base class Collection&lt;T&gt; when an item is added to list;\n        /// registers on new item\n        /// </summary>\n        protected override void InsertItem(int index, XmlElement item)\n        {\n            if (item != null && this.Contains(item))\n            {\n                throw new ArgumentException(SR.Format(SR.XmlNodeAlreadyOwned, \"change\", \"change\"), nameof(item));\n            }\n\n            base.InsertItem(index, item);\n\n            RegisterForElement(item);\n        }\n\n        /// <summary>\n        /// called by base class Collection&lt;T&gt; when an item is added to list;\n        /// unregisters on previous item and registers for new item\n        /// </summary>\n        protected override void SetItem(int index, XmlElement item)\n        {\n            if (item != null && this.Contains(item))\n            {\n                throw new ArgumentException(SR.Format(SR.XmlNodeAlreadyOwned, \"change\", \"change\"), nameof(item));\n            }\n","sourceCodeStart":75,"sourceCodeEnd":111,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/Annotations/XmlElementCollection.cs#L75-L111","documentation":"XmlElementCollection.InsertItem rejects inserting a XmlElement that is already present in the collection, throwing ArgumentException with SR.XmlNodeAlreadyOwned. The collection owns change-record registration per element; an element can only be owned (registered) once, otherwise event registration would double up. Only public API callers supplying duplicates hit this — it guards the annotations store's change-tracking integrity.","triggerScenarios":"Adding the same XmlElement instance twice via the collection's add/Insert (item != null && this.Contains(item)).","commonSituations":"Annotation batch code that re-adds an element already in the collection (e.g. after re-processing a document), or merging two collections that share elements.","solutions":["Check collection.Contains(element) before adding; skip the duplicate.","Remove the element first if re-adding is intended, then add it.","Deduplicate the source list before populating the collection."],"exampleFix":"// before\ncollection.Add(element);\n// after\nif (!collection.Contains(element)) collection.Add(element);","handlingStrategy":"validation","validationCode":"if (element != null && collection.Contains(element))\n    return; // or throw with context","typeGuard":null,"tryCatchPattern":"try { collection.Add(element); }\ncatch (ArgumentException) { /* element already owned — skip */ }","preventionTips":["Deduplicate element lists before populating the collection.","Track ownership explicitly in your document-processing pipeline.","When merging collections, skip elements already present in the target."],"tags":["wpf","annotations","duplicate","xml"],"backgroundTag":"duplicate-entry","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-22T01:17:13.364Z"}