Search code examples
c#htmlxmlxsltasp.net-3.5

Transform XML into HTML using XSLT


I saw several questions related to XML, XSLT and HTML on stackoverflow, but I beleive my question is a little different. Here is what I need:

I need to send an email to my customers. The wordings of the email can differ based upon the scenario. Each scenario has a corresponding format saved in the database. For example, one scenario might require this email:

Scenario 1: We have opened Ticket XXX/Reference Number XXX for your call on XXX. Kindly call us at XXX to track progress.

Another scenario might required this email:

Scenario 2: Thanks for your interest in our Product XXX. As discussed we will send our representative on XXX to your office located at XXX.

Also, the format might need to be altered a bit depending upon data availability. e.g. if I need to send email corresponding to scenario 1, and I don't have Reference Number available, I want to remove the reference number part completely on the fly (not in database) i.e. I want something like this:

Scenario 3: We have opened Ticket XXX for your call on XXX. Kindly call us at XXX to track progress.

The formats for scenarios 1 and 2 are stored in the database as XSLT corresponding to the strings you see above. Format for Scenario 3 has to be produced on the fly. The XXX part has to be replaced with actual data. This data is available in an XML serializable object that I have.

I want to serialize this object, produce an XML in memory, modify the XSLT a little (on the fly) to reflect the data I have, transform the XML in memory to HTML using the XSLT for the scenario and then pass the HTML as a string parameter to an email method I have. The email part works. I need to work on the Object->XML in memory->Slight XSLT modification-> HTML using appropriate XSLT.

I would appreciate if you can include code examples and not just the approach I need to follow.

EDIT:

Here is the working code:

using (xsltStream = new MemoryStream(emailInfo.Body))
            {
                // Create an XmlReader from the Stream
                XmlReader reader = XmlReader.Create(xsltStream);

                // Create and load the transform with document function enabled.
                XslCompiledTransform transform = new XslCompiledTransform();
                XsltSettings settings = new XsltSettings();
                settings.EnableDocumentFunction = true;
                transform.Load(reader, settings, null);

                // Execute the transformation.
                transform.Transform(doc, writer);
             }

Solution

  • Based upon comments from @harpo, @Alexei Levenkov and @Alejandro, I was able to work out a working version of the code which uses multiple templates. Since I can't mark the comments as answers, I will mark this as answer and add the code in my question.

    using (xsltStream = new MemoryStream(emailInfo.Body))
                {
                    // Create an XmlReader from the Stream
                    XmlReader reader = XmlReader.Create(xsltStream);
    
                    // Create and load the transform with document function enabled.
                    XslCompiledTransform transform = new XslCompiledTransform();
                    XsltSettings settings = new XsltSettings();
                    settings.EnableDocumentFunction = true;
                    transform.Load(reader, settings, null);
    
                    // Execute the transformation.
                    transform.Transform(doc, writer);
                 }