Thursday, March 21, 2013

Asp.Net Xml-Xslt Template

Default.aspx
----------------
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="EmailSend.aspx.cs" Inherits="DUMMY_EmailSend" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:Button ID="btnSend" runat="server" Text="Email Template" OnClick="btnSend_Click"/>
    </div>
    </form>
</body>
</html>


Default.aspx.cs
-----------------
protected void btnSend_Click(object sender, EventArgs e)
    {
       // WebRegistration regApplicant = new WebRegistration();
        string ImageOutput = "1";
        bool bEmailSent = GenericEmailProvider.SendRealtorRegistrationAcknowledgementEmail(ImageOutput);
    }


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text;
using System.IO;
/// <summary>
/// Summary description for GenericEmailProvider
/// </summary>
public class GenericEmailProvider
{
    public static bool SendRealtorRegistrationAcknowledgementEmail(string ImageOutput)
    {
        bool bResult = true;
        string WebPageUrl = "http://localhost/TestApp/";
        string xslFileName = HttpContext.Current.Server.MapPath("~/EmailTemplates/XsltTemplates/RealtorsRegistrationAcknowledgement.xslt");
        string xslContent = File.ReadAllText(xslFileName);
        StringBuilder sbHTML = new StringBuilder();
        sbHTML.Append("<Email>");
        if (ImageOutput == "1")
        {
            sbHTML.AppendFormat("<IsUrl>{0}</IsUrl>", "1");
            sbHTML.AppendFormat("<imagepath1>{0}</imagepath1>", "http://www.google.com/Default.aspx?ID=1");
        }
        if (ImageOutput == "0")
        {
            sbHTML.AppendFormat("<IsUrl>{0}</IsUrl>", "0");
            sbHTML.AppendFormat("<imagepath1>{0}</imagepath1>", "http://www.yahoo.com/Default.aspx?ID=1");
        }
        //Test.Mail objMail = new Test.Mail();
        //sbHTML.AppendFormat("<Firstname>{0}</Firstname>", regApplicant.FirstName);
        //bool check = false;
        //sbHTML.AppendFormat("<IsUrlorName>{0}</IsUrlorName>", check ? "1" : "0");
        //sbHTML.AppendFormat("<Lastname>{0}</Lastname>", regApplicant.LastName);
        //sbHTML.AppendFormat("<Reference>{0}</Reference>", regApplicant.AddressId.ToString());
        //sbHTML.AppendFormat("<EmailAddress>{0}</EmailAddress>", regApplicant.Email);
        //sbHTML.AppendFormat("<WebUrl>{0}</WebUrl>", WebPageUrl);
        
        sbHTML.Append("</Email>");
        //objMail.Subject = "test.com - Registration Acknowledgement";
        string transformedEmailText = XslTransformation.TransformXMLtoXsl(sbHTML.ToString(), xslContent);
        //objMail.FromAddress = "noreply@test.com";
        //objMail.To.Add(regApplicant.Email);
        //objMail.Body = transformedEmailText;
        //objMail.SenderName = "noreply@test.com ";
        //try
        //{
        //    objMail.SendEmail(regApplicant.Email, "test.com - Registration Acknowledgement", transformedEmailText);
        //}
        //catch (Exception appError)
        //{
        //    bResult = false;
        //}
        return bResult;
    }
}


XslTransformation.cs
----------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text;
using System.Xml.Xsl;
using System.IO;
using System.Xml;
using System.Xml.XPath;
/// <summary>
/// Summary description for XslTransformation
/// </summary>
public class XslTransformation
{
    public static string TransformXMLtoXsl(string xmlDoc, string xslDoc)
    {
        return XslTransformation.TransformXMLtoXsl(xmlDoc, xslDoc, null);
    }
    public static string TransformXMLtoXsl(string xmlDoc, string xslDoc, Dictionary<string, object> xslArgs)
    {
        StringBuilder sbOutput = new StringBuilder();
        try
        {
            XsltArgumentList argList = new XsltArgumentList();
            if (xslArgs != null)
            {
                foreach (KeyValuePair<string, object> de in xslArgs)
                {
                    argList.AddParam(de.Key, string.Empty, de.Value);
                }
            }
            //read xml string
            TextReader trEmail = new StringReader(xmlDoc);
            XmlTextReader xtrEmailXml = new XmlTextReader(trEmail);
            XPathDocument xmlPathDoc = new XPathDocument(xtrEmailXml);
            //read xsl
            TextReader trXsl = new StringReader(xslDoc);
            XmlTextReader xtrXslEmailTemplate = new XmlTextReader(trXsl);
            XslCompiledTransform xslTransform = new XslCompiledTransform();
            xslTransform.Load(xtrXslEmailTemplate);
            //Output stream
            TextWriter twOut = new StringWriter(sbOutput);
            xslTransform.Transform(xmlPathDoc, argList, twOut);
            xslTransform = null;
        }
        catch (Exception error)
        {
            throw new ApplicationException(string.Format("Transformation Error : {0}", error.Message), error);
        }
        return sbOutput.ToString();
    }
}


EmailTemplates/XsltTemplates/RealtorsRegistrationAcknowledgement.xslt
-----------------------------------------------------------------------
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:template match="/">
    <html xmlns="http://www.w3.org/1999/xhtml">
      <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <title>Domain Authorization Code</title>
        <style type="text/css">            p{ margin-bottom: 12px;            margin-top: 12px;            }            a:link{color:#999;}          </style>
      </head>
      <body>
        <table border="0" cellspacing="0" cellpadding="0" style="width: 100%;">
          <tr>
            <td width="50%">              </td>
            <td style="width: 600px; padding: 20px 15px 0px 15px; background: #ebebeb;">
              <!--MainEmailContent starts here-->
              <table border="0" cellspacing="0" cellpadding="0" style="width: 600px; margin: 20px 0 0 0;                      background: #fff;">
                
                
                <tr>
                  <td>
                    <table width="100%" style="background-color:blue">
                      <tr>
                        <td style="padding: 0 10px 0px; 10px;">
                          <xsl:if test="Email/IsUrl = 0">
                            <xsl:variable name="cname" select="Email/imagepath1"/>
                            <a href="{$cname}">Contact Us</a>
                          </xsl:if>
                          <xsl:if test="Email/IsUrl = 1">
                            <xsl:variable name="cname" select="Email/imagepath1"/>
                            <a href="{$cname}">Contact Us</a>
                          </xsl:if>
                        </td>
                      </tr>
                    </table>
                  </td>
                </tr>
             
              </table>
              <!--Main Email Content Ends -->
            </td>
            <td width="50%">              </td>
          </tr>
        </table>
      </body>
    </html>
  </xsl:template>
</xsl:stylesheet>


 



Output:



-----------



 



image

0 comments:

Post a Comment