1

I have an xml file that is styled with xslt to produce html email template. I get values from users dynamically and replace xml elements text with the values received. How can i get that xml file and send as html email body in c#?

My Xml Looks Like This

  <?xml version="1.0" encoding="utf-8" ?>
  <?xml-stylesheet type="text/xsl" href="EmailTemplateStyleForHTML.xslt"?>
 <EmailTemplate>
   <subject>Information from xyz</subject>
   <displayName>abcd</displayName>
   <Message1>
    Thanks you for registering to xyz.
  </Message1>
  <Copyright>Copyright xyz</Copyright>
 </EmailTemplate>

I am using LINQ to set the values to the xml. Note: I know how to get and set the values from xml but what i want is to grab the whole xml file in to the email body.

If you think there is a better approach for this i would love to hear that. I would really appreciate your help.

Edited after Reply: The xsl transformation part :

            TextReader tr1 = new StringReader(@"EMailTemplateHtml.xml");
            var tr11 = new XmlTextReader(tr1);
            var xPathDocument = new XPathDocument(tr11);

            //read XSLT

            TextReader tr2 = new StringReader(@"EmailTemplateStyleForHTML.xslt");
            var tr22 = new XmlTextReader(tr2);
            var xslt = new XslTransform();
            xslt.Load(tr22);


            var sb = new StringBuilder();
            TextWriter tw = new StringWriter(sb);

            xslt.Transform(xPathDocument, null, tw);

            emailBody = sb.ToString();

I am doing the transformation as you said(@Roy Ashbrook) am i missing anything here?

nzdev
  • 324
  • 1
  • 9
  • 24
  • Some code/pseudo code of the process and where you need to 'get' the xml would probably help. If you are just transforming xml with xslt, it should be pretty simple to just grab the XmlDocument as a string and put it in the body. – Roy Ashbrook Mar 23 '13 at 03:17
  • I am a bit confused during transformation do we need to save the file? – nzdev Mar 23 '13 at 03:21

1 Answers1

0

I believe you will need to actually perform the XSL transform in memory, not reference it in the XML itself. It's possible you could store the XSL in a remote location and reference it that way, but I wouldn't

so:

  1. inject your values into your xml string
  2. transform your xml using your xsl
  3. make that your html message body

Here is some code. Mostly borrowed from/inspired by: How to transform XML as a string w/o using files in .NET? and Sending E-mail using C#.

void Main()
{
    SendHtmlBody(GetHtmlBody());
}
void SendHtmlBody(string HtmlBody){
    using(SmtpClient c = new SmtpClient())
    {
        //set smtp options here
        using(MailMessage msg = new MailMessage("from@replace.me","to@replace.me"))
        {
            msg.Subject = "Testing Bulk mail application";
            msg.Body = HtmlBody;
            msg.IsBodyHtml = true;
            //c.Send(msg);
        }
    }
}
string GetHtmlBody(){
    string xmlInput = @"<?xml version=""1.0"" encoding=""utf-8"" ?>
    <EmailTemplate>
    <subject>Information from xyz</subject>
    <displayName>abcd</displayName>
    <Message1>
        Thanks you for registering to xyz.
    </Message1>
    <Copyright>Copyright xyz</Copyright>
    </EmailTemplate>";

        string xslInput = @"<?xml version=""1.0"" encoding=""ISO-8859-1""?>
    <xsl:stylesheet version=""1.0""
    xmlns:xsl=""http://www.w3.org/1999/XSL/Transform"">
    <xsl:template match=""/"">
    <html>
    <body>
        <h5><xsl:value-of select=""EmailTemplate/subject""/></h5>
        <h5><xsl:value-of select=""EmailTemplate/displayName""/></h5>
    </body>
    </html>
    </xsl:template>
    </xsl:stylesheet>";

    using (StringReader srt = new StringReader(xslInput)) // xslInput is a string that contains xsl
    using (StringReader sri = new StringReader(xmlInput)) // xmlInput is a string that contains xml
    {
        using (XmlReader xrt = XmlReader.Create(srt))
        using (XmlReader xri = XmlReader.Create(sri))
        {
            XslCompiledTransform xslt = new XslCompiledTransform();
            xslt.Load(xrt);
            using (StringWriter sw = new StringWriter())
            using (XmlWriter xwo = XmlWriter.Create(sw, xslt.OutputSettings)) // use OutputSettings of xsl, so it can be output as HTML
            {
                xslt.Transform(xri, xwo);
                return sw.ToString();
            }
        }
    }
}
Community
  • 1
  • 1
Roy Ashbrook
  • 814
  • 8
  • 14
  • hi thank you for your reply i have done the transformation am i missing anything down there? – nzdev Mar 23 '13 at 04:07