I am using XSLT as Email Template for sending email to customer. But i have a problem. my XmlDocument object can not load string builder object value which get from XSLT file with value of param. And i receive an error "Data at the root level is invalid"
. After i copy text in my string builder object value and pass into file.xml then run it in browser and the error come from the text contain unicode value such as "giỏ hàng, đơn hàng ...". Please suggest me some solution to this problem. Thanks in advance
!
here is my code :
public void SendEmail(IDictionary objDictionary, SendMailTypeEnum sendMailEnum)
{
var objXslt = new XslCompiledTransform();
objXslt.Load(EmailTemplate);
var xmlDoc = new XmlDocument();
xmlDoc.AppendChild(xmlDoc.CreateElement("EmailMessage"));
var xpathNav = xmlDoc.CreateNavigator();
var xslArg = new XsltArgumentList();
if (objDictionary != null)
{
foreach (DictionaryEntry entry in objDictionary)
{
xslArg.AddParam(entry.Key.ToString(), "", entry.Value);
}
}
var emailBuilder = new StringBuilder();
var xmlWriter = new XmlTextWriter(new StringWriter(emailBuilder));
objXslt.Transform(xpathNav, xslArg, xmlWriter);
var emailDoc = new XmlDocument();
emailDoc.LoadXml(emailBuilder.ToString());
var titleNode = emailDoc.SelectSingleNode("//title");
if (titleNode == null) return;
var subjectText = titleNode.InnerText;
var bodyNode = emailDoc.SelectSingleNode("//body");
if (bodyNode == null) return;
var bodyText = bodyNode.InnerXml;
if (SendMailTypeEnum.SEND_AUTO_EMAIL.Equals(sendMailEnum))
{
bodyText = HttpUtility.HtmlDecode(bodyText);
}
if (SendMailTypeEnum.SEND_CONTACTUS_EMAIL.Equals(sendMailEnum))
{
bodyText = Utils.EncodeNewLineForward(bodyText);
}
if (SendMailTypeEnum.SEND_ORDER_EMAIL.Equals(sendMailEnum))
{
bodyText = HttpUtility.HtmlDecode(Utils.DecodeForSendOrderMail(bodyText));
}
if (SendMailTypeEnum.SEND_FORUMCOMMENT_EMAIL.Equals(sendMailEnum))
{
bodyText = Utils.EncodeNewLineForward(bodyText);
}
SendEmail(subjectText, bodyText, sendMailEnum);
}
var xmlDoc =newXmlDocument();
xmlDoc.AppendChild(xmlDoc.CreateElement("EmailMessage"));
Your xmlDoc has no root node to append a child to. I dont' have VS open right now but check the intellisense on xmlDoc. There should some Root class there.
var xmlDoc =
new XmlDocument();
xmlDoc.AppendChild(xmlDoc.CreateElement("EmailMessage"));
Your xmlDoc has no root node to append a child to. I dont' have VS open right now but check the intellisense on xmlDoc. There should some Root class there.
Hello adamturner34, thanks for your suggestion but i think the problem do not lie in no root node to append. Because before that, i send my email successfully. But after i add "<xsl:value-ofselect="$Cart"disable-output-escaping="yes"/>" into my xslt file to archieve display html output via value of $Cart param which i pass. Then
the problem occur.
Member
27 Points
128 Posts
XmlDocument LoadXML failing Data at the root level is invalid
Feb 27, 2013 01:07 PM|Ne7ven|LINK
I am using XSLT as Email Template for sending email to customer. But i have a problem. my XmlDocument object can not load string builder object value which get from XSLT file with value of param. And i receive an error "Data at the root level is invalid" . After i copy text in my string builder object value and pass into file.xml then run it in browser and the error come from the text contain unicode value such as "giỏ hàng, đơn hàng ...". Please suggest me some solution to this problem. Thanks in advance !
here is my code :
send email function :
XSLT file :
Contributor
2853 Points
1372 Posts
Re: XmlDocument LoadXML failing Data at the root level is invalid
Feb 27, 2013 01:11 PM|adamturner34|LINK
var xmlDoc = new XmlDocument();
xmlDoc.AppendChild(xmlDoc.CreateElement("EmailMessage"));
Your xmlDoc has no root node to append a child to. I dont' have VS open right now but check the intellisense on xmlDoc. There should some Root class there.
Member
27 Points
128 Posts
Re: XmlDocument LoadXML failing Data at the root level is invalid
Feb 27, 2013 08:03 PM|Ne7ven|LINK
Hello adamturner34, thanks for your suggestion but i think the problem do not lie in no root node to append. Because before that, i send my email successfully. But after i add "<xsl:value-of select="$Cart" disable-output-escaping="yes"/>" into my xslt file to archieve display html output via value of $Cart param which i pass. Then the problem occur.
I will also try your solution. Thanks again !