Click here to Skip to main content
15,435,320 members
Articles / Web Development / ASP.NET
Posted 15 Feb 2005


35 bookmarked

Email Template using XSLT and XSLTArgumentList

Rate me:
Please Sign up or sign in to vote.
3.85/5 (10 votes)
15 Feb 20051 min read
Usage of custom .NET objects in XSLT transformation.

Sample Image


This article uses an XSLT file as a template file to store email templates. It specifically targets on usage of Custom Objects Properties to be used in the XSLT file.


Basic knowledge of XML, XPath and XSLT is needed apart from C# and ASP.NET.

Using the code

The Email.cs class in EmailProject can be segregated into various other components to make it as generic as possible. Also, the User.cs and Account.cs class files can be placed in a separate project containing the business objects for the application.

The following are the extracts from the email.cs file:

public static void SetUserDetails()
  User myuser = new User();
  myuser.UserName = "ABC";
  myuser.EmailAddress =;
  Account myaccount = new Account();
  myaccount.AccountName = "ABC Account";
  Hashtable objHash = new Hashtable();
  objHash["ext:User"] = myuser;
  objHash["ext:Account"] = myaccount;
  SendEmail("", "emailtemplate.xslt", objHash);

The SetUserDetails() function sets the values of User and Account objects. These are objects sent to be used as arguments to the XSLT file. Hence they are added to a HashTable object with the key name as the same as their respective xmlns namespace identifiers in the XSLT file, as shown below.

<?xml version="1.0" encoding="UTF-8" ?>
  <xsl:stylesheet version="1.0" xmlns:xsl=""
XslTransform objxslt = new XslTransform();
objxslt.Load(templatepath + xslttemplatename);
XmlDocument xmldoc = new XmlDocument();
XPathNavigator xpathnav = xmldoc.CreateNavigator();
XsltArgumentList xslarg = new XsltArgumentList();
if (objDictionary != null)
  foreach (DictionaryEntry entry in objDictionary )
    xslarg.AddExtensionObject(entry.Key.ToString(), entry.Value);
  StringBuilder emailbuilder = new StringBuilder();
  XmlTextWriter xmlwriter = new 
    XmlTextWriter(new System.IO.StringWriter(emailbuilder));
  objxslt.Transform(xpathnav, xslarg, xmlwriter, null);

The above is the code snippet from SendMail method. The arguments to the XslTransform object are added by using the AddExtensionObject method of XsltArgumentlist object.

string subjecttext, bodytext;

XmlDocument xemaildoc = new XmlDocument();
XmlNode titlenode = xemaildoc.SelectSingleNode("//title");

subjecttext = titlenode.InnerText;

XmlNode bodynode = xemaildoc.SelectSingleNode("//body");

bodytext = bodynode.InnerXml;
if (bodytext.Length > 0)
    bodytext = bodytext.Replace("&amp","&");

SendEmail(emailto, subjecttext, bodytext);

The above code shows the title text of the XHTML doc to be used as the e-mail's subject and the body text to use used as the body section of the email. This is done by simple XPath.


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

QuestionUnable to get this working. Getting this error: ''get_UserName()' is an unknown XSLT function.' Pin
Member 1226731525-Mar-19 6:49
MemberMember 1226731525-Mar-19 6:49 
GeneralMy vote of 5 Pin
rcat11-Apr-11 5:15
Memberrcat11-Apr-11 5:15 
Generalsource Pin
karadas21-Mar-11 23:19
Memberkaradas21-Mar-11 23:19 
QuestionHow can I generate multiple table? Pin
space257-Nov-06 8:29
Memberspace257-Nov-06 8:29 
GeneralProblem with the href in link Pin
AleBrozzo26-Sep-06 18:43
MemberAleBrozzo26-Sep-06 18:43 
GeneralRe: Problem with the href in link Pin
Mahendra N20-Oct-06 4:09
MemberMahendra N20-Oct-06 4:09 
QuestionHow can I check for null attributes of an associated object? Pin
MG023-Sep-06 5:59
MemberMG023-Sep-06 5:59 
AnswerRe: How can I check for null attributes of an associated object? Pin
MG025-Sep-06 9:30
MemberMG025-Sep-06 9:30 
QuestionHow can I access this key and value in the xslt format? Pin
spacejang1-Aug-06 7:14
Memberspacejang1-Aug-06 7:14 
AnswerRe: How can I access this key and value in the xslt format? Pin
Mahendra N20-Oct-06 4:07
MemberMahendra N20-Oct-06 4:07 
GeneralAccess properties of Associated Objects Pin
jnapier175-May-06 7:09
Memberjnapier175-May-06 7:09 
Generaltaking one more step Pin
the__turtle27-Mar-06 15:20
Memberthe__turtle27-Mar-06 15:20 
GeneralRe: taking one more step Pin
Mahendra N29-Mar-06 18:11
MemberMahendra N29-Mar-06 18:11 
GeneralRe: taking one more step [modified] Pin
Cvetomir Todorov16-Aug-07 21:00
MemberCvetomir Todorov16-Aug-07 21:00 
GeneralRe: taking one more step Pin
cbreier11-Jul-06 5:14
Membercbreier11-Jul-06 5:14 
GeneralRe: taking one more step Pin
the__turtle11-Jul-06 21:52
Memberthe__turtle11-Jul-06 21:52 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.