BlogTalkRadio.Tools.CFT.TransformationTask.Execute C# (CSharp) Method

Execute() public method

Make transformation of file SourceFilePath with transform file TransformFile to destinationFilePath.
public Execute ( string destinationFilePath ) : void
destinationFilePath string File path of destination transformation.
return void
        public void Execute(string destinationFilePath)
        {
            if (string.IsNullOrWhiteSpace(destinationFilePath))
            {
                throw new ArgumentException("Destination file can't be empty.", "destinationFilePath");
            }

            if (string.IsNullOrWhiteSpace(SourceFilePath) || !File.Exists(SourceFilePath))
            {
                throw new FileNotFoundException("Can't find source file.", SourceFilePath);
            }

            if (string.IsNullOrWhiteSpace(TransformFile) || !File.Exists(TransformFile))
            {
                throw new FileNotFoundException("Can't find transform  file.", TransformFile);
            }

            string transformFileContents = File.ReadAllText(TransformFile);

            var document = new XmlDocument();

            try
            {
                document.Load(SourceFilePath);
            }
            catch (Exception ex)
            {
                throw new Exception(string.Format("Error loading source '{0}': {1}", SourceFilePath, ex.Message), ex);
            }

            var transformation = new XmlTransformation(transformFileContents, false, _transformationLogger);

            try
            {
                bool result = transformation.Apply(document);
            }
            catch (Exception ex)
            {
                throw new Exception(string.Format("Error generating '{0}': {1}", destinationFilePath, ex.Message), ex);
            }

            document.Save(destinationFilePath);
        }

Usage Example

Example #1
0
        private bool PerformTransform(string sourceFile, string transformFile, string destinationFile)
        {
            if (!File.Exists(sourceFile))
                throw new FileNotFoundException("The source file was not found", sourceFile);

            var destinationDirectory = Path.GetDirectoryName(destinationFile);

            if (!Directory.Exists(destinationDirectory))
                Directory.CreateDirectory(destinationDirectory);

            // If a transformation file does not exist, just copy sourceFile as the destinationFile
            if (!File.Exists(transformFile))
            {
                var sourceContent = File.ReadAllText(sourceFile);
                File.WriteAllText(destinationFile, sourceContent);
                return false;
            }

            var transformTask = new TransformationTask(sourceFile, transformFile);
            transformTask.Execute(destinationFile);

            return true;
        }