Hi - I just converted an ASP.Net web app from 2.0 to 4.0 and the XslTransform
code that I inherited no longer works in 4.0. I'm told I need to change the code
to use XslCompiledTransform. Fine - but I can't figure out how to get
XslCompiledTransform to do exactly what I need it to do. I have an XSL file in
my project. I do not have an XML or HTML file. The old code read as follows:
[CODE]using (DataSet data = DataGateway.getAbsence(TripNum))
{
data.DataSetName = "Absence";
data.Tables[0].TableName = "Employee";
XmlDataDocument xmldoc = new XmlDataDocument(Data);
XslTransform xslt = new XslTransform;
string xsltfile = "myfile.xsl";
xslt.Load(xsltfile);
TextWriter writer = new StringWriter();
xslt.Transform(xmldoc, null, writer, null);
MailMessage mail = new MailMessage();
mail.Body = writer.ToString();
mail.From = "me";
mail.To = "you";
//more code to send mail......
}[/CODE]I've tried to rewrite this by substituting and XsltCompiledTransform object for
the XslTransform object, and an XmlWriter object for the TextWriter object, but
the xslt.Transform line complains about my using an XmlDataDocument as input. It
suggests an XmlReader instead, but I don't know how to pass the data from my
DataSet into an XmlReader. Or perhaps XslCompiledTransform is not the way to go
Does anyone know?
Thanks!
Loading
Javeed M ShaikhPosted Nov 14, 2011, 12:03 PM
XmlTextReader mXmlTextReader = new XmlTextReader(data.GetXml(),XmlNodeType.Document, null);
An XmlTextReader is an XmlReader, so no casting should be necessary.
Melanie PetersonPosted Nov 14, 2011, 1:57 PM
Javeed M ShaikhPosted Nov 14, 2011, 1:17 PM
SmtpClient smtpClient = new SmtpClient();
MailMessage message = new MailMessage();
try
{
smtpClient.Port = 25;
message.From = new MailAddress("addresshere");
message.To.Add("addresshere");
message.Subject = strSubject;
message.IsBodyHtml = true; //important
string html = "mybody with html tags";
AlternateView htmlView = AlternateView.CreateAlternateViewFromString(html, new ContentType("text/html"));
message.AlternateViews.Add(htmlView);
// Send SMTP mail
smtpClient.Send(message);
}
catch
{
// code for exception
}
Melanie PetersonPosted Nov 14, 2011, 1:08 PM
[CODE]
using (DataSet data = DataGateway.getAbsence(TripNum))
{
data.DataSetName = "Absence";
data.Tables[0].TableName = "Employee";
DataSet data_i = DataGateway.getItinerary(TripNum);
data_i.Tables[0].TableName = "Itinerary";
data.Tables.Add(data_i.Tables[0].Copy());
string xsltfile = System.Configuration.ConfigurationManager.AppSettings["EmailTemplatePath"].ToString() + System.Configuration.ConfigurationManager.AppSettings[EmailTemplateName"].ToString();
XslCompiledTransform xslt = new XslCompiledTransform();
xslt.Load(xsltfile);
TextWriter writer = new StringWriter();
XmlTextReader xmldoc = new XmlTextReader(data.GetXml(), XmlNodeType.Document, null);
xslt.Transform(xmldoc, null, writer);
mail.Body = writer.ToString();
}
[/CODE]
Unfortunately now I have another problem. When the email is sent, it's not formatted properly. All of the HTML tags show. This happened as soon as I converted the project from .Net 2.0 to 4.0 - so it was true using XslTransform also. This is why I decided to convert the code to XslCompiledTransform - I figured the XslTransform, being deprecated, was no longer working properly. Alas, XslCompiledTransform has not solved my problem. This display issue is appears in both Outlook 2003 and 2010 and both are set to display HTML properly. Any idea what could be causing this?
Thanks so much for getting me this far!