string to html tag
-
I am working with .net application. I have few informations in string. Now i would like to put this string into html tags. Lets say i have array of names and i would like to write them down in html paragraph. How could i do this in c# code?
-
I am working with .net application. I have few informations in string. Now i would like to put this string into html tags. Lets say i have array of names and i would like to write them down in html paragraph. How could i do this in c# code?
It depends how you want to format the data. Suppose for instance that you want each name in a paragraph, a quick and dirty hack to do this would be to use something like:
StringBuilder sb = new StringBuilder();
foreach(string name in names)
{
sb.AppendFormat("<p>{0}</p>", name);
}Alternatively you could look at the HtmlTextWriter[^] class to do the job properly.
"WPF has many lovers. It's a veritable porn star!" - Josh Smith
As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.
-
It depends how you want to format the data. Suppose for instance that you want each name in a paragraph, a quick and dirty hack to do this would be to use something like:
StringBuilder sb = new StringBuilder();
foreach(string name in names)
{
sb.AppendFormat("<p>{0}</p>", name);
}Alternatively you could look at the HtmlTextWriter[^] class to do the job properly.
"WPF has many lovers. It's a veritable porn star!" - Josh Smith
As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.
Pete O'Hanlon wrote:
Alternatively you could look at the HtmlTextWriter[^] class to do the job properly.
I think he's better off writing it the frist way. What benefit does the HtmlTextWriter have? I diodn't see any, myself.
.45 ACP - because shooting twice is just silly
-----
"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
-----
"The staggering layers of obscenity in your statement make it a work of art on so many levels." - J. Jystad, 2001 -
Pete O'Hanlon wrote:
Alternatively you could look at the HtmlTextWriter[^] class to do the job properly.
I think he's better off writing it the frist way. What benefit does the HtmlTextWriter have? I diodn't see any, myself.
.45 ACP - because shooting twice is just silly
-----
"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997
-----
"The staggering layers of obscenity in your statement make it a work of art on so many levels." - J. Jystad, 2001I wasn't sure if he was working in an ASP.NET environment. If he is, and he's developing a custom control (again - unsure, so I've gone for the safe case here) then the HtmlTextWriter (and children such as the XhtmlTextWriter) are a good fit.
"WPF has many lovers. It's a veritable porn star!" - Josh Smith
As Braveheart once said, "You can take our freedom but you'll never take our Hobnobs!" - Martin Hughes.
-
I am working with .net application. I have few informations in string. Now i would like to put this string into html tags. Lets say i have array of names and i would like to write them down in html paragraph. How could i do this in c# code?
I would use WebUtility.HtmlEncode from System.NET library. It provides a correct encoding of '<','>' and '&' ,... Your html code would have bugs, if your string contains one of the chars and you don't encode them. You can decorate the converted string with html tags.