There are lot of situations when you have a plain text file and you want to apply certain styles to text and display to the user. In this small example we will take a look at the exam questions file which will contain few questions and we will apply styles to that plain file and display it to the user.
Applying Style to Plain Text Dynamically
Introduction:
There are lot of situations when you have a plain text file and you want to apply certain styles to text and display to the user. In this small example we will take a look at the exam questions file which will contain few questions and we will apply styles to that plain file and display it to the user.
Plain Text File:
Here is the simple text file which consists of questions:
1) What is your
name?
a) AzamSharp *b) Mohammad Azam c) Azim d) Aziz 2) What is your pet name? a) chitta b) bonco *c) coco d) billi 3) What is your city? a) Houston b) Austin *c) Boston d) San antonio
|
The correct answer are represented by '*' following the choices. We will assume that user has given the test and he wants to see the correct answers after the test.
Applying HTML Tags to Plain Text File:
I have created a small class SharpFormat.cs which will apply the styles to the plain text. The complete code for the class is given below:
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(); } }
|
As you can see in the code above that all I am doing is attaching the correct HTML tags at the right time. I am using StringBuilder object to increase performance as using string concatenation will be a bad idea since strings are immutable. I read the text line by line from the file. If I found '*' that means its an answer and I apply different color to the particular row.
The output after applying the styles will look like the screenshot below:
As you can see the screen shot above its looks much better than displaying plain text to user with '*' representing the answer.
I hope you liked the article, happy coding!