public String Transform(String sourceXml, String transformXml) { if (String.IsNullOrEmpty(sourceXml)) { throw new ArgumentNullException("sourceXml"); } if (String.IsNullOrEmpty(transformXml)) { throw new ArgumentNullException("transformXml"); } try { using (var doc = new XmlTransformableDocument()) { logger.Debug($"Beginning transformation"); doc.PreserveWhitespace = this.PreserveWhitespace; doc.LoadXml(sourceXml); using (var xDoc = new XmlTransformation(transformXml, false, null)) { if (xDoc.Apply(doc)) { var builder = new StringBuilder(); var xmlWriterSettings = new XmlWriterSettings { Indent = true, IndentChars = " " }; using (var xmlWriter = XmlTextWriter.Create(builder, xmlWriterSettings)) { doc.WriteTo(xmlWriter); } logger.Debug("Successfully transformed document"); return(builder.ToString()); } else { logger.Warn("Unable to transform - xDoc.Apply failed"); throw new XdtTransformException("Unable to transform"); } } } } catch (Exception ex) { logger.Error(ex, $"Unable to transform, exception: {ex.Message}"); throw new XdtTransformException($"Unable to transform, exception: {ex.Message}", ex); } }
public String Transform(String sourceXml, String transformXml) { if (String.IsNullOrEmpty(sourceXml)) { throw new ArgumentNullException("sourceXml"); } if (String.IsNullOrEmpty(transformXml)) { throw new ArgumentNullException("transformXml"); } try { using (var doc = new XmlTransformableDocument()) { doc.PreserveWhitespace = this.PreserveWhitespace; doc.LoadXml(sourceXml); using (var xDoc = new XmlTransformation(transformXml, false, null)) { if (xDoc.Apply(doc)) { var builder = new StringBuilder(); var xmlWriterSettings = new XmlWriterSettings { Indent = true, IndentChars = " " }; using (var xmlWriter = XmlTextWriter.Create(builder, xmlWriterSettings)) { doc.WriteTo(xmlWriter); } return(builder.ToString()); } else { throw new XdtTransformException("Unable to transform"); } } } } catch (Exception ex) { throw new XdtTransformException("Unable to transform", ex); } }
/// <inheritdoc /> protected override void EndProcessing() { if (OutputPath != null) { using (_configDocument) using (var outConfig = File.Create(OutputPath)) using (var xmlWriter = XmlWriter.Create(outConfig, new XmlWriterSettings() { Indent = true })) { _configDocument.WriteTo(xmlWriter); } } else { WriteObject(_configDocument); } }
// C:\Program Files(x86)\MSBuild\Microsoft\VisualStudio\v11.0\Web\Microsoft.Web.XmlTransform.dll public static bool TransformConfig(string sourcePath, string transformPath, string destinationPath, int maxName = 18, int maxDest = 28) { if (!File.Exists(sourcePath)) { Console.WriteLine("Source file not found: {0}", sourcePath); return(false); } if (!File.Exists(transformPath)) { Console.WriteLine("Transform file not found: {0}", transformPath); return(false); } var document = new XmlTransformableDocument(); document.PreserveWhitespace = false; document.Load(sourcePath); var transformation = new XmlTransformation(transformPath); var status = transformation.Apply(document) ? "" : "Failure: "; Console.Write(" {0}{1,-" + maxName + "} => {2,-" + maxDest + "}", status, Path.GetFileName(transformPath), Path.GetFileName(destinationPath)); var ms = new MemoryStream(); // Apply formatting. var xws = new XmlWriterSettings(); xws.Indent = true; xws.IndentChars = "\t"; xws.CheckCharacters = true; var xw = XmlWriter.Create(ms, xws); document.WriteTo(xw); xw.Close(); var bytes = ms.ToArray(); // If file is missing or different then... if (!File.Exists(destinationPath) || IsDifferent(destinationPath, bytes)) { // Save file. File.WriteAllBytes(destinationPath, bytes); } document.Dispose(); return(true); }
private static void DoTransform(string sourceFile, string transformFile) { using (XmlTransformableDocument document = new XmlTransformableDocument()) using (XmlTransformation transformation = new XmlTransformation(transformFile)) { document.PreserveWhitespace = true; document.Load(sourceFile); var success = transformation.Apply(document); if (success) { var settings = new XmlWriterSettings { Encoding = Encoding.UTF8 }; var writer = XmlWriter.Create(Console.Out, settings); document.WriteTo(writer); writer.Flush(); } } }
public ActionResult Create(string webConfigXml, string transformXml) { try { using (var document = new XmlTransformableDocument()) { document.PreserveWhitespace = true; document.LoadXml(webConfigXml); using (var transform = new XmlTransformation(transformXml, false, null)) { if (transform.Apply(document)) { var stringBuilder = new StringBuilder(); var xmlWriterSettings = new XmlWriterSettings(); xmlWriterSettings.Indent = true; xmlWriterSettings.IndentChars = " "; using (var xmlTextWriter = XmlTextWriter.Create(stringBuilder, xmlWriterSettings)) { document.WriteTo(xmlTextWriter); } return(Content(stringBuilder.ToString(), "text/xml")); } else { return(ErrorXml("Transformation failed for unkown reason")); } } } } catch (XmlTransformationException xmlTransformationException) { return(ErrorXml(xmlTransformationException.Message)); } catch (XmlException xmlException) { return(ErrorXml(xmlException.Message)); } }
static void Main(string[] args) { var opts = new Options(); if (!CommandLine.Parser.Default.ParseArguments(args, opts)) { return; } var dict = new Dictionary <string, string>(); if (!File.Exists(opts.InputFile)) { Console.WriteLine("Input file {0} doesn't exists", opts.InputFile); return; } if (!File.Exists(opts.TransformFile)) { Console.WriteLine("Transform file {0} doesn't exists", opts.TransformFile); return; } foreach (var item in opts.Arguments) { if (item.IndexOf("=") < 1) { Console.WriteLine("Invalid key-value pair: {0}", item); Console.WriteLine(opts.GetHelp()); return; } else { var index = item.IndexOf("="); var key = item.Substring(0, index); var value = item.Substring(index + 1); dict.Add(key, value); } } TransformDocument transform; using (var fs = new FileStream(opts.TransformFile, FileMode.Open, FileAccess.Read)) { transform = TransformDocument.LoadFrom(fs); } transform.ApplyArguments(dict); var doc = new XmlTransformableDocument(); doc.Load(opts.InputFile); transform.ApplyTo(doc); using (var writer = XmlWriter.Create(opts.OutputFile, new XmlWriterSettings { Indent = true })) { doc.WriteTo(writer); } }
private static int Main(string[] args) { if (args.Length != 3) { Console.WriteLine("Required Arguments: [ConfigPath] [TransformPath] [TargetPath]"); return(400); } WriteVersion(); var configPath = args[0]; if (!File.Exists(configPath)) { Console.WriteLine($"Config not found {configPath}"); return(404); } var transformPath = args[1]; if (!File.Exists(transformPath)) { Console.WriteLine($"Transform not found {transformPath}"); return(404); } try { var targetPath = args[2]; var configXml = File.ReadAllText(configPath); var transformXml = File.ReadAllText(transformPath); Console.WriteLine($"Source File: {configPath}"); Console.WriteLine($"Transform: {transformPath}"); Console.WriteLine($"Target: {targetPath}"); using (var document = new XmlTransformableDocument()) { document.PreserveWhitespace = true; document.LoadXml(configXml); using (var transform = new XmlTransformation(transformXml, false, null)) { if (transform.Apply(document)) { var stringBuilder = new StringBuilder(); var xmlWriterSettings = new XmlWriterSettings { Indent = true, IndentChars = " " }; using (var xmlTextWriter = XmlWriter.Create(stringBuilder, xmlWriterSettings)) { document.WriteTo(xmlTextWriter); } var resultXml = stringBuilder.ToString(); File.WriteAllText(targetPath, resultXml); return(0); } Console.WriteLine("Transformation failed for unknown reason"); } } } catch (XmlTransformationException xmlTransformationException) { Console.WriteLine(xmlTransformationException.Message); } catch (XmlException xmlException) { Console.WriteLine(xmlException.Message); } catch (Exception ex) { Console.WriteLine(ex.Message); } return(500); }