Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

I have a list that accepts emails and creates list items out of them.

The issue I'm facing is that if someone emails the list using plain text, the formatting showing up in a list item's multi-line text column is barely readable (no newlines or anything).

I want to be able to translate plain text into HTML so that the multi-line rich text field reflects its formatting, in C#.

I've tried the HttpUtility.HtmlEncode but the formatting is still lost.

share|improve this question
add comment

1 Answer 1

public string parseText(string text, bool allow)
{
    //Create a StringBuilder object from the string input parameter            
    StringBuilder sb = new StringBuilder(text);

    //Replace all double white spaces with a single white space and  
    sb.Replace(" ", " ");

    //Check if HTML tags are not allowed
    if (!allow)
    {
        //Convert the brackets into HTML equivalents
        sb.Replace("<", "&lt;");
        sb.Replace(">", "&gt;");
        //Convert the double quote
        sb.Replace("\"", "&quot;");
    }
    //Create a StringReader from the processed string of the StringBuilder
    StringReader sr = new StringReader(sb.ToString());
    StringWriter sw = new StringWriter();
    //Loop while next character exists
    while (sr.Peek() > -1)
    {
        //Read a line from the string and store it to a temp variable
        string temp = sr.ReadLine();
        //write the string with the HTML break tag
        //Note here write method writes to a Internal StringBuilder
        //object created automatically
        sw.Write(temp + "<br>");
    }
    //Return the final processed text
    return sw.GetStringBuilder().ToString();
}

Source: http://www.c-sharpcorner.com/UploadFile/saurabhnandu/TexttoHTMLParser12032005072354AM/TexttoHTMLParser.aspx

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.