Click here to Skip to main content
12,691,637 members (31,178 online)
Click here to Skip to main content
Articles » Languages » XML » XML/XSLT » Downloads


29 bookmarked

Template Messages Using XSL Transformations and XML Serialization

, 23 Aug 2007 CPOL
This article shows how you can implement templated messages, such as email templates
using System;
using System.IO;
using System.Windows.Forms;

namespace TemplatesExample
    public partial class MainForm : Form
        public MainForm()

        private void ButtonBrowse_Click(object sender, EventArgs e)
            DialogResult dialogResult = openFileDialogXSLTFile.ShowDialog();
            if (dialogResult == DialogResult.OK)
                textBoxXsltFile.Text = openFileDialogXSLTFile.FileName;

        private void ButtonTransform_Click(object sender, EventArgs e)
            string outputFileName = AskUserForOutputFile();
            if (outputFileName == null)

            Transformer transformer = CreateTransformer();
            ProductsOrderMailData data = CreateMailData();
            Stream outputFile = new FileStream(outputFileName, FileMode.Create);

            transformer.Transform(data, outputFile);
            string uriFilename = string.Concat(Uri.UriSchemeFile, Uri.SchemeDelimiter, outputFileName);
            webBrowserResult.Url = new Uri(uriFilename);

        private string AskUserForOutputFile()
            string outputFilename = null;
            DialogResult dialogResult = saveFileDialogOutputFile.ShowDialog();
            if (dialogResult == DialogResult.OK)
                outputFilename = saveFileDialogOutputFile.FileName;

            return outputFilename;

        private Transformer CreateTransformer()
            Stream xsltFile = new FileStream(textBoxXsltFile.Text, FileMode.Open);
            Transformer transformer = new Transformer();
            transformer.Initialize(xsltFile, true);
            transformer.ObjectSerialized += WriteSerializedObjectToFileStream;
            return transformer;

        private static void WriteSerializedObjectToFileStream(object sender, ObjectSerializedEventArgs e)
            StreamReader reader = new StreamReader(e.SerializedObject);
            Stream fileStream = new FileStream("object.xml", FileMode.Create);
            StreamWriter writer = new StreamWriter(fileStream);

            string allData = reader.ReadToEnd();

        private static ProductsOrderMailData CreateMailData()
            ProductsOrderMailData data = new ProductsOrderMailData();

            Product soap = CreateProduct(1, "melon soap", (decimal)2.3);
            Product shampoo = CreateProduct(2, "shampoo", (decimal)5.5);
            Product towel = CreateProduct(5, "cotton towel", 15);

            data.CustomerName = "Peter";
            data.OrderId = 12321;
            data.OrderDate = DateTime.UtcNow;

            return data;

        private static Product CreateProduct(int id, string name, decimal price)
            Product product = new Product();

            product.Id = id;
            product.Name = name;
            product.Price = price;

            return product;

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Cvetomir Todorov
Software Developer
Bulgaria Bulgaria
Cvetomir Todorov is a student in the Sofia University. His interests include Object Oriented Programming and Design, Design Patterns, Producing High-Quality Code, Refactoring, Unit Testing, Test Driven Development etc. The technology he's using is .NET with C# language.

You may also be interested in...

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170117.1 | Last Updated 23 Aug 2007
Article Copyright 2007 by Cvetomir Todorov
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid