public void AddElement(ISchemaElement element) { foreach (object obj in this.items) { bool containerUsed; CollectionItem collectionItem = obj as CollectionItem; if (collectionItem != null) { containerUsed = collectionItem.Elements.Count != 0; if (collectionItem.ElementType.IsAssignableFrom(element.GetType())) { collectionItem.AddElement(element); if (!containerUsed) { this.containersUsed++; } this.totalContainedItems++; return; } continue; } ElementCollection collection = obj as ElementCollection; if (collection != null) { containerUsed = collection.Count != 0; try { collection.AddElement(element); if (!containerUsed) { 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)); }