5,442,164 members and growing! (21,083 online)
Email Password   helpLost your password?
Languages » XML » XSLT     Intermediate

Email Template using XSLT and XSLTArgumentList

By Mahendra_Nepali

Usage of custom .NET objects in XSLT transformation.
C#, XML, Windows, .NET 1.1, .NET, ASP.NET, Visual Studio, VS.NET2003, Dev

Posted: 16 Feb 2005
Updated: 16 Feb 2005
Views: 29,630
Bookmarked: 23 times
Announcements
Want a new Job?



Search    
Advanced Search
Sitemap
7 votes for this Article.
Popularity: 2.84 Rating: 3.36 out of 5
1 vote, 14.3%
1
1 vote, 14.3%
2
1 vote, 14.3%
3
0 votes, 0.0%
4
4 votes, 57.1%
5

Sample Image

Introduction

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.

Background

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 = abc@someemail.com;
  Account myaccount = new Account();
  myaccount.AccountName = "ABC Account";
  Hashtable objHash = new Hashtable();
  objHash["ext:User"] = myuser;
  objHash["ext:Account"] = myaccount;
  SendEmail("xyz@email.com", "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="http://www.w3.org/1999/XSL/Transform"
   xmlns:UserDetails="ext:User"
     xmlns:Account="ext:Account">
XslTransform objxslt = new XslTransform();
objxslt.Load(templatepath + xslttemplatename);
XmlDocument xmldoc = new XmlDocument();
xmldoc.AppendChild(xmldoc.CreateElement("DocumentRoot"));
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();
xemaildoc.LoadXml(emailbuilder.ToString());
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.

License

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

About the Author

Mahendra_Nepali



Location: Ireland Ireland

Other popular XML articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 13 of 13 (Total in Forum: 13) (Refresh)FirstPrevNext
Subject  Author Date 
GeneralHow can I generate multiple table?memberspace259:29 7 Nov '06  
GeneralProblem with the href in linkmemberAleBrozzo19:43 26 Sep '06  
GeneralRe: Problem with the href in linkmemberMahendra N5:09 20 Oct '06  
GeneralHow can I check for null attributes of an associated object?memberSG026:59 3 Sep '06  
GeneralRe: How can I check for null attributes of an associated object?memberSG0210:30 5 Sep '06  
QuestionHow can I access this key and value in the xslt format?memberspacejang8:14 1 Aug '06  
AnswerRe: How can I access this key and value in the xslt format?memberMahendra N5:07 20 Oct '06  
GeneralAccess properties of Associated Objectsmemberjnapier178:09 5 May '06  
Generaltaking one more stepmembery6y6y16:20 27 Mar '06  
GeneralRe: taking one more stepmemberMahendra N19:11 29 Mar '06  
GeneralRe: taking one more step [modified]memberCvetomir Todorov22:00 16 Aug '07  
GeneralRe: taking one more stepmembercbreier6:14 11 Jul '06  
GeneralRe: taking one more stepmemberthe__turtle22:52 11 Jul '06  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 16 Feb 2005
Editor: Smitha Vijayan
Copyright 2005 by Mahendra_Nepali
Everything else Copyright © CodeProject, 1999-2008
Web08 | Advertise on the Code Project