You are here:   Home
  |  Login

Programmer Newsletter

Minimize

Master Programmer

Minimize

Find articles on computer software, programming, ASP.net, Sql server, databases, C#, websites, Internet, Windows, Outlook macros.

Programming Articles

Minimize
13

You may get some html or xml text that you want to display as is. To do this, you need to convert the text so that the web browser does not format the html.

You need to change the < and > characters to the html codes &lt; and &gt;. You also should add html line breaks <br /> where there are line breaks in the original text.

Here is Asp.net C# code.

public string html_code_gt = "&gt;";
public string html_code_lt = "&lt;";
public string html_gt = ">";
public string html_lt = "<";
public string html_newline = "<br />";
public string newline = "\n";
//------------------------------------
public string Html_To_Html(string text);
  text = text.Replace(html_gt, html_code_gt);
  text = text.Replace(html_lt, html_code_lt);
  text = text.Replace(newline, newline + html_newline);
return text;

If you are getting your original code from user entered text, you may want to let your user specify additional line breaks in the text by entering \n where they want the new line.

This can serve as a method all to itself for converting line breaks to html.

Notice that when converting the user entered \n, the code only adds a <br />, but when converting an actual newline char, the code adds both the newline and the <br />. This is too more closely reflect what the source text actually looks like.

public string html_newline = "<br />";
public string newline = "\n"; // Real new line
public string newline_text = @"\n"; // User entered /n
//------------------------------------
public string Replace_Newline(string text);
  text = text.Replace(newline_text, html_newline);
  text = text.Replace(newline, newline + html_newline);
return text;

You can then call this method in your html to html method.

public string html_code_gt = "&gt;";
public string html_code_lt = "&lt;";
public string html_gt = ">";
public string html_lt = "<";
public string html_newline = "<br />";
public string newline = "\n"; // Real new line
public string newline_text = @"\n"; // User entered /n
//------------------------------------ public string Html_To_Html(string text); text = text.Replace(html_gt, html_code_gt); text = text.Replace(html_lt, html_code_lt); text = Replace_Newline(text); return text;

By Andrew Weitzen, Bronze Inc. (c) 2010

Bronze is the publisher of several online Internet journals including: InternetHandholding.com, DomainNames.gs, DotNetNuke.bz, Programmer.bz, Software.vg, WebHosting.vg

Posted in: Asp.net

Post Rating

Comments

Anonymous User
# Anonymous User
Thursday, September 16, 2010 5:42 PM
Ustream Live Video, Trust, Embed Facebook Video, Ebook Marketing

Post Comment

Only registered users may post comments.

Programmer Newsletter

Minimize

Subscribe to the Internet Handholding newsletter



Reccomend Programmer.bz

Minimize

Share/Bookmark Bookmark and Share