Answered by:
Converting Plain text to HTML in C#

Question
-
Hi!
How can I convert Plain Text to HTML formatted text using C# code?
Please help. I am a beginner in C# programming.
Thanks in advance.
2ak7Saturday, December 10, 2011 2:12 PM
Answers
-
Well, if your code generates the string "This is a paragraph.", then change your code to generate a string "<p>This is a paragraph.</p>".It's hard to say exactly how to change your code. For example, if I was building an HTML email, I might have several sections that I wrote using a StringWriter object, formatting each section. Something like the following:StringWriter sw = new StringWriter();sw.WriteLine("<p>{0}</p>", "This is a paragraph");
--
Mike- Marked as answer by Leo Liu - MSFTModerator Tuesday, December 20, 2011 4:52 AM
Saturday, December 10, 2011 8:09 PM
All replies
-
This is imho not a C# problem as the solution is quite the same in almost any programming language. E.g.
string plainText = "Plain Text value"; string htmlText = plainText;
There is no difference per se. Plain text stays plain text. Maybe you want to embed plain text into an HTML page:
string plainText = "Plain Text value"; string htmlPage = "<HTML><BODY>" + plainText + "</BODY></HTML>";
Saturday, December 10, 2011 2:21 PM -
There are so many possible answers to this, perhaps it is better to describe your goal, rather than your approach. Off the top of my head, the text you describe may appear inside any of the following tags in HTML, depending on your goal: <p>, <h1>, <span>, <div>, <a>, <textarea>, <input>.
--
MikeSaturday, December 10, 2011 2:37 PM -
My goal is to be able to send emails in HTML formatted text.
Currently, my mails are being sent as Plain Text because of which I am having the problem of line breaks after 78 characters. I came to know that if I send emails using HTML formatted text instead, then I can turn off the Autowrap property which causes the line breaks.
- Edited by ylk56 Saturday, December 10, 2011 3:43 PM
Saturday, December 10, 2011 2:56 PM -
OUCH! Stefan Hoffmann's answer of "<HTML><BODY>" + plainText + "</BODY></HTML>" is very wrong, no offence.
You have to sanitize your plaintext before treating it as HTML. <'s will be treated as tag openers and other HTML entities must also be escaped. < becomes < for example.
See this article for instructions.
Also, line breaks and other whitespace will be treated differently in HTML. Multiple whitespace characters are treated as one, and line breaks are no different from spaces. You will have to provide your own line breaking using markup. But it seems like you are at least partly interested in having line-breaking and word wrapping be handled for you by default HTML behaviour.
<P> tags, <BR> tags and <PRE> tags will help you achieve the basics of what you want. But you will have to process your plain text to search for line endings and replace them suitable markup. This is not really an automatic thing, and should be done to your own specification to suit your requirements.
Saturday, December 10, 2011 4:03 PM -
Well, the "p" tag is for paragraphs, and sounds like what you would likely use. Have you set the BodyFormat tag of the MailMessage to MailFormat.HTML?
--
MikeSaturday, December 10, 2011 4:11 PM -
-
No I haven't set the BodyFormat tag to MailFormat.HTML because currently my message body doesn't contain any HTML tags. I read somewhere that if I am going to send as HTML format, then it is suggested that I only use basic character formatting commands to ensure compatibility with different mail readers.
<P>, <BR> and <PRE> are the basic tags that I want to include in my message body. Can anyone give any example of how to process the Plain text and replace them with these markups?
Saturday, December 10, 2011 5:00 PM -
For example:<p>This is a paragraph that I want the text to wrap when it extents past the width of the email window.</p>To break a line forcefully, use this.<br/><pre>This text will be formatted by you, including breaks and tabs.So this line would be indented relative to the line before.</pre>
--
MikeSaturday, December 10, 2011 5:06 PM -
Sorry for not being specific but unfortunately I wasn't looking for this example. I wanted an example of how to process Plain Text using C# code and convert it to HTML.
- Edited by ylk56 Saturday, December 10, 2011 5:39 PM
Saturday, December 10, 2011 5:38 PM -
Well, if your code generates the string "This is a paragraph.", then change your code to generate a string "<p>This is a paragraph.</p>".It's hard to say exactly how to change your code. For example, if I was building an HTML email, I might have several sections that I wrote using a StringWriter object, formatting each section. Something like the following:StringWriter sw = new StringWriter();sw.WriteLine("<p>{0}</p>", "This is a paragraph");
--
Mike- Marked as answer by Leo Liu - MSFTModerator Tuesday, December 20, 2011 4:52 AM
Saturday, December 10, 2011 8:09 PM -
http://www.mastercsharp.com/Article/28/text-to-html-parser
take this link as reffernce,maybe will help you,good luck
http://mysftway.blogspot.com/ 熱誠熱心地幫忙大家! 希望與大家切磋技術哦~Saturday, December 10, 2011 9:24 PM