public void RemoveElement(ISchemaElement element) { foreach (object obj in this.items) { CollectionItem collectionItem = obj as CollectionItem; if (collectionItem != null) { if (collectionItem.ElementType.IsAssignableFrom(element.GetType())) { if (collectionItem.Elements.Count == 0) { return; } collectionItem.RemoveElement(element); if (collectionItem.Elements.Count == 0) { this.containersUsed--; } this.totalContainedItems--; return; } continue; } ElementCollection collection = obj as ElementCollection; if (collection != null) { if (collection.Count == 0) { continue; } try { collection.RemoveElement(element); if (collection.Count == 0) { this.containersUsed--; } this.totalContainedItems--; return; } catch (ArgumentException) { // Eat the exception and keep looking. We'll throw our own if we can't find its home. } continue; } } throw new ArgumentException(String.Format( CultureInfo.InvariantCulture, "Element of type {0} is not valid for this collection.", element.GetType().Name)); }