Click here to Skip to main content
Click here to Skip to main content
Articles » Languages » XML » XML/XSLT » Downloads
 
Add your own
alternative version

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()
        {
            InitializeComponent();
        }

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

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

            transformer.Transform(data, outputFile);
            outputFile.Close();
            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();
            writer.Write(allData);
            writer.Close();
        }

        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;
            data.Products.Add(soap);
            data.Products.Add(shampoo);
            data.Products.Add(towel);

            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.

License

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

Share

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.

| Advertise | Privacy | Mobile
Web04 | 2.8.141015.1 | Last Updated 23 Aug 2007
Article Copyright 2007 by Cvetomir Todorov
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid