Geeks With Blogs
AzamSharp Some day I will know everything. I hope that day never comes.

Sometimes we need to add HTML Tags to the plain text and give them a better display. A good example will be if someone sends you a plain text file containing multiple choice questions. The file will look something like this:

Plain Text File:

1) What is your name and tell me where you live?

a) AzamSharp
*b) Mohammad Azam
c) Azim
d) Aziz

2) What is your pet name?

a) chitta
b) bonco
*c) coco
d) billi

(The '*' denotes the right answer).

Now you want to parse this file and add HTML to it so that it will look nicer when it displays on the screen. For this purpose I have made a SharpFormat class library whose job is to make the text pretty.

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Text; 


public class SharpFormat
{
    
public const string TABLE_BEGIN_TAG = @"<table name=""myTable"" border=""2"">";
    
public const string TABLE_END_TAG = @"</table>";
    
public const string TR_OPEN_TAG = @"<tr><td>";
    
public const string TR_CLOSE_TAG = @"</td></tr>";
    
public const string TR_OPEN_TAG_COLOR = @"<tr bgcolor=""green""><td>";
        
        
    
public SharpFormat()
    {
        
    }

    
// This method formats the text and sends back the formatted text
    
public static string FormatText(string fileName)
    {
        FileStream fileStream = File.OpenRead(HttpContext.Current.Server.MapPath(fileName));
        StreamReader reader = 
new StreamReader(fileStream);
        
string nextLine = null;
        StringBuilder sb = 
new StringBuilder(); 

        
try
        
{
            
// Add the Table beginning tag 
            
sb.Append(TABLE_BEGIN_TAG);
            
            
            
while (reader.Peek() > -1)
            {
                nextLine = reader.ReadLine();

                
if (nextLine.StartsWith("*"))  // * means answer 
                
{
                    sb.Append(TR_OPEN_TAG_COLOR);
                    sb.Append(nextLine);
                    sb.Append(TR_CLOSE_TAG);
                }
                
else
                
{
                    sb.Append(TR_OPEN_TAG);
                    sb.Append(nextLine);
                    sb.Append(TR_CLOSE_TAG);
                }
            }
        }
        
finally
        
{
            reader.Close();
            fileStream.Close();
        }

        sb.Append(TABLE_END_TAG);       
        
        
return sb.ToString();
    }
}

When you pass the filename which contains the Plain Text to the SharpFormat.FormatText method it will return the text with the HTML tags which when displayed on the screen looks something like this:

:)

powered by IMHO

 

Posted on Thursday, October 27, 2005 6:23 PM | Back to top


Comments on this post: Adding HTML Tags to the plain text at runtime

No comments posted yet.
Your comment:
 (will show your gravatar)


Copyright © Mohammad Azam | Powered by: GeeksWithBlogs.net