internal override void SaveChanges(XmlDocument document, XmlNamespaceManager namespaces) { var containerElement = (XmlElement)document.SelectSingleNode("/cpix:CPIX/cpix:" + ContainerName, namespaces); if (Count == 0 && SignedBy.Count() == 0) { // We don't have any contents to put in it AND we don't have any signatures to apply. // This is the only scenario where we do not need the container element in the document, so remove it // and consider the save operation completed. All other paths follow the longer logic chain. if (containerElement != null) { containerElement.ParentNode.RemoveChild(containerElement); } return; } // We need a container element, so create one if it is missing. if (containerElement == null) { var rootElement = document.DocumentElement; // This namespace must exist, as the root element itself uses it. We will reuse the same prefix. var prefix = rootElement.GetPrefixOfNamespace(Constants.CpixNamespace); var element = document.CreateElement(prefix, ContainerName, Constants.CpixNamespace); containerElement = XmlHelpers.InsertTopLevelCpixXmlElementInCorrectOrder(element, document); } // Add any new items and then mark them as loaded items. foreach (var item in _newItems.ToArray()) { var element = SerializeEntity(document, namespaces, containerElement, item); _newItems.Remove(item); _loadedItemsData.Add(new Tuple <TEntity, XmlElement>(item, element)); } SaveNewSignatures(document, containerElement); }