Click here to Skip to main content
13,088,846 members (54,626 online)
Click here to Skip to main content
Articles » Languages » XML » XML/XSLT » Downloads


29 bookmarked
Posted 23 Aug 2007

Template Messages Using XSL Transformations and XML Serialization

, 23 Aug 2007
This article shows how you can implement templated messages, such as email templates
using System;
using System.IO;
using System.Xml;
using System.Xml.Serialization;
using System.Xml.Xsl;

namespace TemplatesExample
    internal class Transformer
        private XslCompiledTransform mXslTransformer;
        private event EventHandler<ObjectSerializedEventArgs> mObjectSerialized;

        public event EventHandler<ObjectSerializedEventArgs> ObjectSerialized
            add { mObjectSerialized += value; }
            remove { mObjectSerialized -= value; }

        private void RaiseObjectSerializedEvent(Stream serializedObject)
            EventHandler<ObjectSerializedEventArgs> objectSerialized = mObjectSerialized;

            if (objectSerialized != null)
                Stream cloning = CloneStream(serializedObject);
                ObjectSerializedEventArgs args = new ObjectSerializedEventArgs(cloning);
                objectSerialized(this, args);

        private static Stream CloneStream(Stream original)
            original.Position = 0;
            Stream cloning = new MemoryStream();

            const int bufferSize = 1024;
            byte[] buffer = new byte[bufferSize];
            int readBytes;
            while ((readBytes = original.Read(buffer, 0, bufferSize)) > 0)
                cloning.Write(buffer, 0, readBytes);

            cloning.Position = 0;
            return cloning;

        public void Initialize(Stream styleSheetStream, bool closeStream)
            XmlReader styleSheetReader = null;
                styleSheetReader = XmlReader.Create(styleSheetStream);
                mXslTransformer = new XslCompiledTransform();
                if (closeStream)
                    CloseStreamAndReader(styleSheetStream, styleSheetReader);

        private static void CloseStreamAndReader(Stream stream, XmlReader reader)
            if (reader != null)

        public void Transform(object xmlSerializableParameter, Stream outputStream)
            if (mXslTransformer == null)
                throw new InvalidOperationException("You must first initialize the transformer before you can use it.");

            Stream serializationStream = SerializeObject(xmlSerializableParameter);
            XmlReader reader = XmlReader.Create(serializationStream);
            XmlWriter writer = XmlWriter.Create(outputStream);

            mXslTransformer.Transform(reader, writer);

        private Stream SerializeObject(object parameter)
            Stream serializationStream = new MemoryStream();
            XmlSerializer serializer = new XmlSerializer(parameter.GetType());
            serializer.Serialize(serializationStream, parameter);


            serializationStream.Position = 0;
            return serializationStream;

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...

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