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.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;
            try
            {
                styleSheetReader = XmlReader.Create(styleSheetStream);
                mXslTransformer = new XslCompiledTransform();
                mXslTransformer.Load(styleSheetReader);
            }
            finally
            {
                if (closeStream)
                    CloseStreamAndReader(styleSheetStream, styleSheetReader);
            }
        }

        private static void CloseStreamAndReader(Stream stream, XmlReader reader)
        {
            if (reader != null)
            {
                reader.Close();
            }
            stream.Close();
        }

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

            RaiseObjectSerializedEvent(serializationStream);

            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.

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 | Terms of Use | Mobile
Web03 | 2.8.1411022.1 | Last Updated 23 Aug 2007
Article Copyright 2007 by Cvetomir Todorov
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid