private static XElement CreateUpdatedXElement(IXElementWrapper wrapper, XElement originalElement)
        {
            XElement result = new XElement(originalElement.Name);

            foreach (XAttribute attribute in originalElement.Attributes())
            {
                result.Add(new XAttribute(attribute.Name, attribute.Value));
            }

            wrapper.CommitData(result);

            return(result);
        }
        public void Update(IEnumerable <IData> dataset)
        {
            Verify.ArgumentNotNull(dataset, "dataset");

            CheckTransactionNotInAbortedState();

            using (XmlDataProviderDocumentCache.CreateEditingContext())
            {
                var validatedFileRecords = new Dictionary <DataSourceId, FileRecord>();
                var validatedElements    = new Dictionary <DataSourceId, XElement>();

                // verify phase
                foreach (IData wrappedData in dataset)
                {
                    var data = DataWrappingFacade.UnWrap(wrappedData);

                    Verify.ArgumentCondition(data != null, "dataset", "Collection contains a null element.");
                    Verify.ArgumentCondition(data.DataSourceId != null, "dataset", "Collection contains a data item with DataSourceId null property.");

                    ValidationHelper.Validate(data);

                    XmlDataTypeStore dataTypeStore = _xmlDataTypeStoresContainer.GetDataTypeStore(data.DataSourceId.InterfaceType);


                    var dataScope = data.DataSourceId.DataScopeIdentifier;
                    var culture   = data.DataSourceId.LocaleScope;
                    var type      = data.DataSourceId.InterfaceType;

                    var dataTypeStoreScope = dataTypeStore.GetDataScope(dataScope, culture, type);

                    dataTypeStore.Helper.ValidateDataType(data);

                    if (null == data.DataSourceId)
                    {
                        throw new ArgumentException("The DataSourceId property of the data argument must not be null", "data");
                    }
                    if (data.DataSourceId.ProviderName != _dataProviderContext.ProviderName)
                    {
                        throw new ArgumentException("The data element does not belong to this provider", "data");
                    }

                    var fileRecord = GetFileRecord(dataTypeStore, dataTypeStoreScope);

                    var index = fileRecord.RecordSet.Index;

                    IDataId dataId = data.DataSourceId.DataId;

                    XElement element = index[dataId];

                    Verify.ArgumentCondition(element != null, "dataset", "No data element corresponds to the given data id");

                    IXElementWrapper wrapper = data as IXElementWrapper;
                    Verify.ArgumentCondition(wrapper != null, "dataset", "The type of data was expected to be of type {0}".FormatWith(typeof(IXElementWrapper)));

                    XElement updatedElement = CreateUpdatedXElement(wrapper, element);

                    validatedFileRecords.Add(data.DataSourceId, fileRecord);
                    validatedElements.Add(data.DataSourceId, updatedElement);
                }

                foreach (var key in validatedElements.Keys)
                {
                    FileRecord fileRecord = validatedFileRecords[key];
                    fileRecord.Dirty = true;
                    fileRecord.RecordSet.Index[key.DataId] = validatedElements[key];
                }

                XmlDataProviderDocumentCache.SaveChanges();

                SubscribeToTransactionRollbackEvent();
            }
        }