public void SetTextContent(string text) { // encode CDATA end tags text = text.Replace("]]>", "]]]]><![CDATA[>"); // strip invalid xml chars text = XmlSanitizer.SanitizeXmlString(text).Replace("&", string.Empty); text = System.Security.SecurityElement.Escape(text); Content = "<pre>" + text + "</pre>"; }
public void AddAttachment(byte[] binaryData, string contentId, string contentType, string contentFileName) { byte[] hash = new MD5CryptoServiceProvider().ComputeHash(binaryData); string hashHex = BitConverter.ToString(hash).Replace("-", string.Empty).ToLower(); string cid = string.Empty; if (contentId != null) { cid = contentId.TrimStart('<', '"'); } cid = cid.TrimEnd('>', '"'); if ((cid.Length > 0) && content.Contains(cid)) { // convert the reference tag to a media tag int idIndex = content.IndexOf(cid); // go left until the '<' is found int bracketIndex = content.LastIndexOf('<', idIndex); int endBracket = content.IndexOf('>', bracketIndex); string refTag = content.Substring(bracketIndex, endBracket - bracketIndex + 1); int srcStart = refTag.ToLower().IndexOf("src=\"") + 4; int srcEnd = refTag.IndexOf('"', srcStart + 1); string srcString = refTag.Substring(srcStart, srcEnd - srcStart + 1); string mediaTag = refTag.Replace("src=", "hash="); mediaTag = mediaTag.Replace(srcString, '"' + hashHex + '"'); int imgStart = mediaTag.IndexOf('<') + 1; int imgEnd = mediaTag.IndexOfAny(" \t".ToCharArray(), imgStart); mediaTag = mediaTag.Remove(imgStart, imgEnd - imgStart); mediaTag = mediaTag.Insert(imgStart, "en-media type=\"" + contentType.ToLower() + "\""); Content = content.Replace(refTag, mediaTag); } else { // just link the attachment to the content Content = content + "<en-media hash=\"" + hashHex + "\" type=\"" + contentType.ToLower() + "\"/>"; } string attachmentString = Convert.ToBase64String(binaryData); attachmentString = "<data encoding=\"base64\">" + attachmentString + "</data><mime>" + contentType.ToLower() + "</mime>" + "<resource-attributes><file-name>" + XmlSanitizer.SanitizeXmlString(contentFileName).Replace("&", string.Empty) + "</file-name></resource-attributes>"; Attachment at = new Attachment(); at.Base64Data = attachmentString; at.ContentID = contentId; at.ContentType = contentType.ToLower(); at.FileName = contentFileName; at.Hash = hashHex; Attachments.Add(at); }