Пример #1
0
        public void TemplateSerializationClean()
        {
            string dirPath  = Path.GetDirectoryName(Uri.UnescapeDataString(new UriBuilder(Assembly.GetExecutingAssembly().CodeBase).Path));
            string filePath = Path.Combine(dirPath, "Resources", "TemplateTest.txt");
            string template = File.ReadAllText(filePath);

            var vars = new Dictionary <string, string>
            {
                { "CoolStuff", "I'm cool" },
                { "IsMyBuddy", "True" },
                { "BigNumber", "25" },
                { "Min1", "21" },
                { "Max1", "25" },
                { "Min2", "42" },
                { "Max2", "50" },
                { "MyCar", "Subaru" },
                { "DoraCar", "Kia" },
            };
            var arrays = new Dictionary <string, IEnumerable <string> >
            {
                { "Chocolate", new[] { "Dark", "White", "75%", "Milk", "Very Dark" } },
                { "Fruit", new[] { "Orange", "Lemon", "Pineapple", "Strawberry", "Kiwi" } },
                { "Person", new[] { "Billy", "Bob", "Julia" } }
            };

            string resFilePath    = Path.Combine(dirPath, "Resources", "TemplateTestResultClean.txt");
            string expectedResult = File.ReadAllText(resFilePath);

            Assert.AreEqual(expectedResult, XmTemplateSerializer.Serialize(XmTemplateDeserializer.Deserialize(template), vars, arrays));
        }
Пример #2
0
        public static string ExecuteTemplate(string templateStr, StringWriter sw, IDictionary <string, string> variables = null, IDictionary <string, IEnumerable <string> > arrays = null)
        {
            var template = XmTemplateDeserializer.Deserialize(templateStr);

            //Act
            var arr    = arrays ?? new Dictionary <string, IEnumerable <string> >();
            var vars   = variables ?? new Dictionary <string, string>();
            var result = XmTemplateSerializer.Serialize(template, vars, arr);

            sw.WriteLine();
            sw.WriteLine(templateStr);
            foreach (var x in vars)
            {
                sw.WriteLine("{0} = {1}", x.Key, x.Value);
            }
            foreach (var array in arr)
            {
                sw.WriteLine("{0} = ({1})", array.Key, string.Join(", ", array.Value));
            }
            return(result);
        }