Hai,
I am trying to read a text file and write the text in a html file using c#. But when I opened the html file in the browser the special characters got collapsed.
Here is the text file content:
A slightly vainer factor to take into account is how you look to your contemporaries and other players on the course – if you look good, you’ll feel good and that will increase your confidence and performance on the day.
This is output I got in the browser:
A slightly vainer factor to take into account is how you look to your contemporaries and other players on the course � if you look good, you�ll feel good and that will increase your confidence and performance on the day.
Can anybody tell me how to handle this.
This is my c# code
fi1 = new FileInfo("C:/Documents and Settings/anusha.ESPL/Desktop/mytext.txt");FileInfo
if (fi1.Exists)
{
StreamReader sr = new StreamReader(fi1.FullName);
string content = sr.ReadToEnd();
sr.Close();
FileInfo fi2 = new FileInfo("C:/Documents and Settings/anusha.ESPL/Desktop/Untitled-1.htm");
if (fi2.Exists)
{
StreamWriter sw = new StreamWriter(fi2.FullName);
sw.Write(content);
sw.Close();
}
}
Thanks
Anusha.
anushaPosted Apr 14, 2009, 6:00 AM
Hai Lisa,
Thanku, I tried both u r codes. But it's not working.
Now the content changes like this:
other players on the course ? if you look good, you?ll feel good and that will increase your confidence and performance on the day.
Can u please find any other solution.
Anusha.
LisaPosted Apr 14, 2009, 5:39 AM
there are coupl of ways to do it
either you use the following function to completely convert them to html encodes
public string HTMLEncodeSpecialChars(string text)
{
System.Text.StringBuilder sb = new System.Text.StringBuilder();
foreach (char c in text){
if(c>127) // special chars
sb.Append(String.Format("&#{0};",(int)c));
else
sb.Append(c);
}
return sb.ToString();
}
or System.Web.HttpUtility.HtmlEncode() can also help in your case
Thanks,
Lisa