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);
            }
        }
Exemple #3
0
        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;
            }

            var contentsBeforeTransformation = File.ReadAllBytes(opts.InputFile);

            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);

            var outputStream = new MemoryStream();
            using (var writer = XmlWriter.Create(outputStream, new XmlWriterSettings {Indent = true}))
            {
                doc.WriteTo(writer);
            }

            var contentsAfterTransformation = outputStream.ToArray();

            if (!contentsBeforeTransformation.SequenceEqual(contentsAfterTransformation))
            {
                Console.WriteLine("Updating '{0}'", opts.OutputFile);
                outputStream.Position = 0;
                using (var fs = File.Create(opts.OutputFile))
                {
                    outputStream.CopyTo(fs);
                    fs.Close();
                }
            }
            else
            {
                Console.WriteLine("No changes made");
            }
        }