static void Main(string[] args) { if (args.Length != 3) { Console.WriteLine("Wrong number of arguments"); Console.WriteLine("WebConfigTransformer ConfigFilename TransformFilename ResultFilename"); Environment.Exit(1); } if (!System.IO.File.Exists(args[0]) && !System.IO.File.Exists(args[1])) { Console.WriteLine("The config or transform file do not exist!"); Environment.Exit(2); } using (var doc = new Microsoft.Web.XmlTransform.XmlTransformableDocument()) { doc.Load(args[0]); using (var tranform = new Microsoft.Web.XmlTransform.XmlTransformation(args[1])) { if (tranform.Apply(doc)) { doc.Save(args[2]); } else { Console.WriteLine("Could not apply transform"); Environment.Exit(3); } } } }
public static string Transform(string webConfigStr, string transformConfigStr, string outputConfigStr, string outPutFileName) { using (var doc = new Microsoft.Web.XmlTransform.XmlTransformableDocument()) { doc.Load(webConfigStr); using (var transform = new Microsoft.Web.XmlTransform.XmlTransformation(transformConfigStr)) { if (transform.Apply(doc)) { if (!Directory.Exists(outputConfigStr)) { Directory.CreateDirectory(outputConfigStr); } doc.Save(Path.Combine(outputConfigStr, outPutFileName)); return("Transformation successful"); } else { return("Transformation failed"); } } } }
public void XmlTransform_Support_CommentOut() { string src = CreateATestFile("Web.config", Resources.Web); string transformFile = CreateATestFile("Web.Test.config", Resources.Web_Test); string destFile = GetTestFilePath("MyWeb.config"); string keyName = "keyAppSettings1"; //execute Microsoft.Web.XmlTransform.XmlTransformableDocument x = new Microsoft.Web.XmlTransform.XmlTransformableDocument(); x.PreserveWhitespace = true; x.Load(src); // Verify the XML content has the requested XML node var xPathString = $"/configuration/appSettings/add[@key='{keyName}']"; System.Xml.XmlNodeList nodesFound = x.SelectNodes(xPathString); Assert.NotNull(nodesFound); Assert.NotEmpty(nodesFound); Microsoft.Web.XmlTransform.XmlTransformation transform = new Microsoft.Web.XmlTransform.XmlTransformation(transformFile); bool succeed = transform.Apply(x); FileStream fsDestFile = new FileStream(destFile, FileMode.OpenOrCreate); x.Save(fsDestFile); //verify, we have a success transform Assert.True(succeed); //sanity verify the content is right, (xml was transformed) fsDestFile.Close(); x.Dispose(); x = new Microsoft.Web.XmlTransform.XmlTransformableDocument(); x.PreserveWhitespace = true; x.Load(destFile); // Verify the XML content does not expose the requested (already commented out) XML node nodesFound = x.SelectNodes(xPathString); Assert.NotNull(nodesFound); Assert.Empty(nodesFound); List <string> lines = new List <string>(File.ReadLines(destFile)); //sanity verify the line format is not lost (otherwise we will have only one long line) Assert.True(lines.Count > 10); //be nice transform.Dispose(); x.Dispose(); }
private bool TransformWebConfig(string webConfigPath, string transformationFilePath) { var config = new Microsoft.Web.XmlTransform.XmlTransformableDocument(); config.PreserveWhitespace = true; config.Load(webConfigPath); var transformation = new Microsoft.Web.XmlTransform.XmlTransformation(transformationFilePath); if (transformation.Apply(config)) { config.Save(webConfigPath); return(true); } else { return(false); } }
public void XmlTransform_Support_WriteToStream() { string src = CreateATestFile("Web.config", "Web.config"); string transformFile = CreateATestFile("Web.Release.config", "Web.Release.config"); string destFile = GetTestFilePath("MyWeb.config"); //execute Microsoft.Web.XmlTransform.XmlTransformableDocument x = new Microsoft.Web.XmlTransform.XmlTransformableDocument(); x.PreserveWhitespace = true; x.Load(src); Microsoft.Web.XmlTransform.XmlTransformation transform = new Microsoft.Web.XmlTransform.XmlTransformation(transformFile); bool succeed = transform.Apply(x); FileStream fsDestFile = new FileStream(destFile, FileMode.OpenOrCreate); x.Save(fsDestFile); //verify, we have a success transform Assert.AreEqual(true, succeed); //verify, the stream is not closed Assert.AreEqual(true, fsDestFile.CanWrite, "The file stream can not be written. was it closed?"); //sanity verify the content is right, (xml was transformed) fsDestFile.Dispose(); string content = File.ReadAllText(destFile); Assert.IsFalse(content.Contains("debug=\"true\"")); List <string> lines = new List <string>(File.ReadLines(destFile)); //sanity verify the line format is not lost (otherwsie we will have only one long line) Assert.IsTrue(lines.Count > 10); //be nice transform.Dispose(); x.Dispose(); }
public void XmlTransform_Support_WriteToStream() { string src = CreateATestFile("Web.config", Properties.Resources.Web); string transformFile = CreateATestFile("Web.Release.config", Properties.Resources.Web_Release); string destFile = GetTestFilePath("MyWeb.config"); //execute Microsoft.Web.XmlTransform.XmlTransformableDocument x = new Microsoft.Web.XmlTransform.XmlTransformableDocument(); x.PreserveWhitespace = true; x.Load(src); Microsoft.Web.XmlTransform.XmlTransformation transform = new Microsoft.Web.XmlTransform.XmlTransformation(transformFile); bool succeed = transform.Apply(x); FileStream fsDestFile = new FileStream(destFile, FileMode.OpenOrCreate); x.Save(fsDestFile); //verify, we have a success transform Assert.AreEqual(true, succeed); //verify, the stream is not closed Assert.AreEqual(true, fsDestFile.CanWrite, "The file stream can not be written. was it closed?"); //sanity verify the content is right, (xml was transformed) fsDestFile.Close(); string content = File.ReadAllText(destFile); Assert.IsFalse(content.Contains("debug=\"true\"")); List<string> lines = new List<string>(File.ReadLines(destFile)); //sanity verify the line format is not lost (otherwsie we will have only one long line) Assert.IsTrue(lines.Count>10); //be nice transform.Dispose(); x.Dispose(); }