private void btnUpdateTransformationPreview_Click(object sender, RoutedEventArgs e) { try { int columnIndex = datastore.Metadata.Columns[this.configuration.InputXmlColumn].ColumnIndex; string originalDataStore = (string)datastore[0][columnIndex]; int originalRowCount = datastore.Count; tbTransformedXml.Text = XmlTransformation.TransformXml(tbInputData.Text, this.configuration.TransformationXslt); var transformationLog = XmlTransformation.TransformToDatastore(datastore, this.configuration.TransformationXslt, this.configuration.InputXmlColumn, true); this.DataPreviewGrid.DataContext = DatastoreHelper.ConvertDatastoreToTable(this.datastore, 10000, new string[] { this.configuration.InputXmlColumn }, transformationLog.RowNumbersToHide.ToArray()); foreach (var newColumn in transformationLog.NewColumns) { datastore.RemoveColumn(newColumn); } for (int rowIndex = datastore.Count - 1; rowIndex != originalRowCount - 1; rowIndex--) { datastore.RemoveDataAt(rowIndex); } datastore[0][columnIndex] = originalDataStore; } catch (Exception ex) { string errorMessage = "An error occured while transforming:\n\n" + ex.Message; if (ex.InnerException != null) { errorMessage += ex.InnerException.Message; } MessageBox.Show(errorMessage); } }
public static TransformationLog TransformToDatastore(IDatastore datastore, string xslTransformation, string columnToTransform, bool isRunFromPreview) { TransformationLog transformationLog = new TransformationLog(); if (datastore.Count <= 0) { return(transformationLog); } int columnIndex = datastore.Metadata.Columns[columnToTransform].ColumnIndex; int rowCount = datastore.Count; // Rowcount increases while adding data, therefore it must be fixed here! // Transform xml to table? string transformedXml = TransformXml(datastore[0][columnIndex].ToString(), xslTransformation); if (Regex.IsMatch(transformedXml, @"<\?xml.*\?>.*\n<it_table>")) { for (int sourceRowIndex = 0; sourceRowIndex < rowCount; sourceRowIndex++) { transformedXml = TransformXml(datastore[sourceRowIndex][columnIndex].ToString(), xslTransformation); XDocument xDocument = XDocument.Parse(transformedXml, LoadOptions.PreserveWhitespace); foreach (var row in xDocument.Root.Elements("it_row")) { AddDatarowToDatastore(datastore, transformationLog, sourceRowIndex, row); } } // Remove transformed rows if (isRunFromPreview == false) { for (int rowIndex = rowCount - 1; rowIndex >= 0; rowIndex--) { datastore.RemoveDataAt(rowIndex); } datastore.RemoveColumn(datastore.Metadata.Columns.Where(t => t.Value.ColumnIndex == columnIndex).First().Value.ColumnName); } else { for (int rowIndex = rowCount - 1; rowIndex >= 0; rowIndex--) { transformationLog.RowNumbersToHide.Add(rowIndex); } } } else { for (int rowIndex = 0; rowIndex < rowCount; rowIndex++) { datastore.SetValue(rowIndex, columnIndex, TransformXml(datastore[rowIndex][columnIndex].ToString(), xslTransformation)); } } return(transformationLog); }