private TransformResults GetTransformResults()
        {
            var results = new TransformResults {
                Successful = true
            };
            var log = new StringBuilder();
            var transformFilePaths = new List <string>();

            string baseFolder = GetBaseFolder();

            string baseFilePath = FindFilePath(baseFolder, _possibleBaseFilePaths, log);

            if (baseFilePath == null)
            {
                results.Successful = false;
            }
            else
            {
                log.AppendLine($"Base XML file is {baseFilePath}");
                baseFolder = new FileInfo(baseFilePath).DirectoryName;

                foreach (string[] possibleTransformPaths in _transformPathsArray)
                {
                    string transformFilePath = FindFilePath(baseFolder, possibleTransformPaths, log);
                    if (transformFilePath == null)
                    {
                        results.Successful = false;
                        break;
                    }
                    else
                    {
                        results.FilePath = transformFilePath;
                        transformFilePaths.Add(transformFilePath);
                    }
                }
            }

            if (results.Successful)
            {
                ApplyTransformations(baseFilePath, transformFilePaths.ToArray(), results, log);
            }
            else
            {
                results.ErrorMessage = "File not found.";
            }

            results.Log = log.ToString();
            return(results);
        }
        /// <summary>
        /// Perform XML transformation(s).
        /// </summary>
        /// <returns>The <see cref="TransformResults"/>.</returns>
        public TransformResults Transform()
        {
            lock (_transformResultsLock)
            {
                string key = BuildResultsKey();

                if (_transformResults.ContainsKey(key))
                {
                    GotTransformResultsFromCache = true;
                    return(_transformResults[key]);
                }

                GotTransformResultsFromCache = false;
                TransformResults results = GetTransformResults();
                _transformResults.Add(key, results);
                return(results);
            }
        }
        private void ApplyTransformations(
            string baseFilePath,
            string[] transformFilePaths,
            TransformResults results,
            StringBuilder log)
        {
            using (var transformableDocument = new XmlTransformableDocument {
                PreserveWhitespace = true
            })
            {
                log.AppendLine($"Loading {baseFilePath} to XmlTransformableDocument...");

                transformableDocument.Load(baseFilePath);

                foreach (string transformFilePath in transformFilePaths)
                {
                    using (var sr = new StreamReader(transformFilePath))
                        using (var transformation = new XmlTransformation(sr.BaseStream, new TransformationLogger(log)))
                        {
                            log.AppendLine($"\nApplying transformation file {transformFilePath}...");

                            if (transformation.Apply(transformableDocument))
                            {
                                log.AppendLine("Transformation Successful!");
                            }
                            else
                            {
                                log.AppendLine("Transformation Failed.");
                                results.Successful   = false;
                                results.ErrorMessage = $"Transformation failed for file \"{transformFilePath}\".";
                                break;
                            }
                        }
                }

                if (results.Successful)
                {
                    results.TransformedXml = RemoveXmlNamespaces(transformableDocument.OuterXml);
                    results.XDocument      = XDocument.Parse(results.TransformedXml, LoadOptions.PreserveWhitespace);
                }
            }
        }