Extract content from html code at code behind
Hi All,
I have a field coming from database, it is a html code.
sample:
"html tag"extract content from html code at code behind"/html tag""html tag"did have solution?"/html tag"
At code behind I need to count the text, such like "extract content from html code at code behind" + "did have solution?". if the length exceeds the limit, do something.
How can I get the real text without HTML tag in my cs file?
Thank you in advance.

Kirtan PatelRank #292 · 8.6K reputationPosted Oct 9, 2009, 12:44 AM EDT
dont forget to mark "Do you like this answer" please it will give me some credits :)
private void button1_Click(object sender, EventArgs e)
{
string HTML = "Content Is Here
another content";
Regex re = new Regex("<[a-z0-9A-Z/\'\"= ]*>");
MatchCollection mc = re.Matches(HTML);
foreach (Match m in mc)
{
HTML = HTML.Replace(m.ToString(),"");
}
MessageBox.Show(HTML);
MessageBox.Show(HTML.Length.ToString());
//Do the operation you want to do with Length
}
Roei BarRank #1,271 · 1.4K reputationPosted Oct 8, 2009, 8:31 AM EDT
or you could use a regex
Kirtan PatelRank #292 · 8.6K reputationPosted Oct 8, 2009, 9:01 AM EDT
Could not understood what you are telling .. can you explain a bit more .:)
tell me what exactly output you want Explain with example :)
i will help you out :)
Ellen Hu55 reputationPosted Oct 8, 2009, 9:34 AM EDT
btw, Regex.Replace(feed.Content, "<(.|\n)+?>"," "); let me get the result.
here is an example: (you may not see the html tags because browser renders it)
Linda Smith commented on AMERICAN HEART ASSOCIATION INC
Now I have the text of "Linda Smith commented on AMERICAN HEART ASSOCIATION INC" by using regex.replace; since the string length exceeds the limit, I used substring to cut, and display "Linda Smith commented on AMERICAN HEART ASSOC..." on UI.
However, I have the right length of string, the link is gone. It should have 2 links.
here is my part of code:
string strContent;
strContent = Regex.Replace(feed.Content, "<(.|\n)+?>"," ");
if (strContent.Length > 50)
{
ltContent.Text = String.Format(CultureInfo.InvariantCulture, "{0}...", strContent.Substring(1, 49));
}
else
{
ltConten = feed.content;
}
How cau I keep the link and cut the length?
Amit ChoudharyRank #145 · 15.8K reputationPosted Oct 8, 2009, 11:31 PM EDT
because the first and the last symbols are < and > contains the whole if look deeply...
try to use FirstIndexOf() and LastIndexOf() method to get the result right.
Ellen Hu55 reputationPosted Oct 9, 2009, 7:20 AM EDT
Finally I decided to make changes on my trigger, to get those string length take cared before inserting to table. Thank you again.