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 PatelPosted Oct 9, 2009, 4:44 AM
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
}
Ellen HuPosted Oct 9, 2009, 11:20 AM
Finally I decided to make changes on my trigger, to get those string length take cared before inserting to table. Thank you again.
Amit ChoudharyPosted Oct 9, 2009, 3:31 AM
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 HuPosted Oct 8, 2009, 1:34 PM
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?
Kirtan PatelPosted Oct 8, 2009, 1:01 PM
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 :)
Roei BarPosted Oct 8, 2009, 12:31 PM
or you could use a regex