473,398 Members | 2,389 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

Removing markup from html string

Hi,

I'm using WebClient to retrieve the contents of a particular page. I
would like to get a string containing only the page's text and no html
markup.

How can I do this? Is there a class to take care of this?

Many thanks!

Keith

Nov 17 '05 #1
3 2264
Regex

"keith" wrote:
Hi,

I'm using WebClient to retrieve the contents of a particular page. I
would like to get a string containing only the page's text and no html
markup.

How can I do this? Is there a class to take care of this?

Many thanks!

Keith

Nov 17 '05 #2
"keith" <ke********@gmail.com> wrote in
news:11*********************@f14g2000cwb.googlegro ups.com:
Hi,

I'm using WebClient to retrieve the contents of a particular
page. I would like to get a string containing only the page's
text and no html markup.

How can I do this? Is there a class to take care of this?

Many thanks!

Keith

Keith,

Here's a method that does that:
/// <summary>
/// Given a string containing HTML/XML/SGML tags, this method strips
/// out all of the tags and returns the remaining text.
/// </summary>
/// <param name="html">
/// The HTML/XML/SGML to search.
/// </param>
/// <returns>
/// The <c>html</c> text stripped of all tags. If <c>html</c> is null or empty,
/// then <c>html</c> is returned.
/// </returns>
public static string GetTextStrippedOfHtml(string html)
{
if ((html == null) || (html.Trim().Length == 0))
return html;

return Regex.Replace(html, @"
< # Tag's opening less-than sign.
[^>]+? # One or more characters that aren't a tag's closing greater-than sign (non-greedy). # Tag's closing greater-than sign.",

string.Empty,
RegexOptions.Singleline |
RegexOptions.IgnoreCase |
RegexOptions.IgnorePatternWhitespace);
}
--
Hope this helps.

Chris.
-------------
C.R. Timmons Consulting, Inc.
http://www.crtimmonsinc.com/
Nov 17 '05 #3
Thanks guys!

Chris great code, works like a charm.

Keith

Nov 17 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: James T Jordan | last post by:
Hi, I am quite frustrated with the problem I was given to solve. I have xsl as input which outputs html but some of the html is coded like &lt;tag&gt and I need to remove the html formatting from the...
11
by: rajarao | last post by:
hi I want to remove the content embedded in <script> and </script> tags submitted via text box. My java script should remove the content embedded between <script> and </script> tag. my current...
47
by: Andy Dingley | last post by:
Assume an English language page, linking to a foreign history resource that's only available in a foreign language. Any suggestions on appropriate "best practice" markup, particularly regarding the...
16
by: graham.reeds | last post by:
I am updating a website that uses a countdown script embedded on the page. When the page is served the var's are set to how long the countdown has left in minutes and seconds, but the rest of the...
3
by: Michal A. Valasek | last post by:
Hello, I want to transform text with HTML markup to plain text. Is there some simple way how to do it? I can surely write my own function, which would simply strip everything with < and >....
0
by: tommyw | last post by:
Hi everybody, I'm new to codecomments, but I really hope someone can help me. I am dynamically loading custom controls and user controls into my htmlform control. As I have a lot of "custom"...
6
by: bruce | last post by:
hi... i'm running into a problem where i'm seeing non-ascii chars in the parsing i'm doing. in looking through various docs, i can't find functions to remove/restrict strings to valid ascii...
1
by: dave8421 | last post by:
Hi, I have a (strict) html document with the following portion: <ul> <li><img alt="" src="images/image1.jpg" /></li> <li><img alt="" src="images/image2.jpg" /></li> <li><img alt=""...
1
by: Gordon | last post by:
I've been looking into including a system to allow logging of diffs so users can view different versions of a file and roll it back to an earlier version. After a lot of faffing with PHP diff...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.