dotnet/wpf · error · ArgumentException

SR.Format(SR.XmlNodeAlreadyOwned, "change", "change")

Error message

SR.Format(SR.XmlNodeAlreadyOwned, "change", "change")

What it means

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.

Solutions

  1. Check collection.Contains(element) before adding; skip the duplicate.
  2. Remove the element first if re-adding is intended, then add it.
  3. Deduplicate the source list before populating the collection.

Example fix

// before
collection.Add(element);
// after
if (!collection.Contains(element)) collection.Add(element);
Defensive patterns

Strategy: validation

Validate before calling

if (element != null && collection.Contains(element))
    return; // or throw with context

Try / catch

try { collection.Add(element); }
catch (ArgumentException) { /* element already owned — skip */ }

Prevention

When it happens

Trigger: Adding the same XmlElement instance twice via the collection's add/Insert (item != null && this.Contains(item)).

Common situations: 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.

Related errors


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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/Annotations/XmlElementCollection.cs:93

        /// </summary>
        protected override void RemoveItem(int index)
        {
            XmlElement removedItem = this[index];

            UnregisterForElement(removedItem);

            base.RemoveItem(index);
        }

        /// <summary>
        /// called by base class Collection&lt;T&gt; when an item is added to list;
        /// registers on new item
        /// </summary>
        protected override void InsertItem(int index, XmlElement item)
        {
            if (item != null && this.Contains(item))
            {
                throw new ArgumentException(SR.Format(SR.XmlNodeAlreadyOwned, "change", "change"), nameof(item));
            }

            base.InsertItem(index, item);

            RegisterForElement(item);
        }

        /// <summary>
        /// called by base class Collection&lt;T&gt; when an item is added to list;
        /// unregisters on previous item and registers for new item
        /// </summary>
        protected override void SetItem(int index, XmlElement item)
        {
            if (item != null && this.Contains(item))
            {
                throw new ArgumentException(SR.Format(SR.XmlNodeAlreadyOwned, "change", "change"), nameof(item));
            }

View on GitHub (pinned to 81131a70a4)