Today I intended to process the email description field which contains the body of an email, but got nerved by CRM’s logic.
Out of the box CRM does wrap the email body with:
<pre class="mscrmpretag">YOUR CONTENT</pre>
My plugin code did not expect that as I planned to parse the email’s body of certain email types as XML string (customer’s requirement).
To work around this you can use this fix helper class using your own implementation of your deserialized entity class:
public static class EmailXmlHandler { /// <summary> /// Gets a serialized Incident instance from a XML string. /// </summary> /// <param name="xmlString">The XML string.</param> /// <returns>Incident instance</returns> public static Incident GetIncidentFromString(string xmlString, ITracingService tracer) { XDocument xmlDoc = null; Incident incident = null; tracer.Trace("EmailXmlHandler.GetIncidentFromString(): string content: '{0}'", xmlString); // remove the crm default pre tag if (!string.IsNullOrWhiteSpace(xmlString) && xmlString.StartsWith("<pre class=\"mscrmpretag\"")) { tracer.Trace("EmailXmlHandler.GetIncidentFromString(): removing mscrmpretag and decoding content"); xmlString = xmlString.Replace("<pre class=\"mscrmpretag\">", "").Replace("</pre>", ""); string tag = "/INCIDENT>"; int pos = xmlString.IndexOf(tag); xmlString = xmlString.Substring(0, pos + tag.Length); xmlString = System.Net.WebUtility.HtmlDecode(xmlString); tracer.Trace("EmailXmlHandler.GetIncidentFromString(): successfully removed mscrmpretag and decoded content"); } if (!string.IsNullOrWhiteSpace(xmlString) && xmlString.StartsWith("<?xml version=\"1.0\" encoding=\"utf-8\"?>")) { tracer.Trace("EmailXmlHandler.GetIncidentFromString(): Start deserialization of XML string."); xmlDoc = XDocument.Parse(xmlString); incident = Deserialize<Incident>(xmlDoc); tracer.Trace("EmailXmlHandler.GetIncidentFromString(): Finished deserialization of XML string."); } return incident; } /// <summary> /// Deserializes the specified document. /// </summary> /// <typeparam name="T"></typeparam> /// <param name="doc">The document.</param> /// <returns></returns> public static T Deserialize<T>(XDocument doc) { XmlSerializer xmlSerializer = new XmlSerializer(typeof(T)); using (var reader = doc.Root.CreateReader()) { return (T)xmlSerializer.Deserialize(reader); } } }
Comments
Post a Comment