How to strip a string of , ,, , ,

,
tags?
I have stream which is the HTML input of a page. Now I want to use only that part of this page, that is within 
and
tags, and excluding these tags.
How would I go about stripping , ,, , ,
,
, and tags
? I have to make sure that and its corresponding tags are not stripped in this process.
sun21170
0
sun21170
8/17/2005 5:58:52 PM
📁 asp.net.getting-started
📃 91979 articles.
⭐ 4 followers.

💬 11 Replies
👁️‍🗨️ 2225 Views



   Wait... you said you wanted to strip the head tags, and you're also saying you need to NOT strip the head tags.  What do you need?
   What if you enclose everything in a div or span tag, just inside the form tags... and then use Javascript to get myDiv.innerHTML?  Would that get everything you need?
Cheers,

Peter Brunone
MS MVP, ASP.NET
Founder, EasyListBox.com
Do the impossible, and go home early.
0
PeterBrunone
8/17/2005 7:23:29 PM

Here's a function to strip out all html tags...
 
private string StripHTML(string htmlString){
string pattern = @"";
return Regex.Replace(htmlString,pattern,string.Empty);
}
From this, you need to adjust it or pull out from the entire code what you want to strip and put into the function, then add it back.
Not exactly what you need, but a good start.
Zath
0
Zath
8/17/2005 7:33:59 PM
I'm assuming that doesn't handle HTML encoded characters as well...?


Peter Brunone
MS MVP, ASP.NET
Founder, EasyListBox.com
Do the impossible, and go home early.
0
PeterBrunone
8/17/2005 7:41:20 PM
The point is I did not want the javascript within head tags to be stripped because then the response stream will not render or function correctly in a browser.

sun21170
0
sun21170
8/17/2005 10:15:24 PM

I suppose now would be a good time to ask (a) where this HTML is coming from, (b) where it is going, and (c) where it will be processed, i.e. client or server.
 

Peter Brunone
MS MVP, ASP.NET
Founder, EasyListBox.com
Do the impossible, and go home early.
0
PeterBrunone
8/18/2005 1:02:51 AM
Peter,

The answers to your questions are:
(a) I am getting the html response for a web page on a different web server. For example, if my site is xxx.com, then from within the code behind of <a href="http://www.xxx.com/getresults.aspx">www.xxx.com/getresults.aspx</a> , I am getting the final html response of  <a href="http://www.yy.com/findindexes.aspx">www.yy.com/findindexes.aspx</a>. I use WebClient class for this. Then I want to display the output in my getresults.aspx page which is an empty page except for the code-behind that requests the remote page and then receives that remote page's html output. Note that these url's are hypothetical ones so don't expect to get anything when you click on them.
(b) As you must have guessed its coming from a remote server i.e. <a href="http://www.yy.com/findindexes.aspx">www.yy.com/findindexes.aspx</a>
(c) Its going to become the final ouput of  <a href="http://www.xxx.com/getresults.aspx">www.xxx.com/getresults.aspx</a>
The problem is if I add the remote page's html output to my receiving page I get an error since there will be two forms, two html tags, two head tags and two body tags. Actually I would be very happy if somehow I could make the receving page have no tags to start with because then I could simply write the remote response into the receiving page without creating double tags.

sun21170
0
sun21170
8/18/2005 1:16:04 AM
Note that these url's are hypothetical ones so don't expect to get anything when you click on them.

Uh... thanks
Actually I would be very happy if somehow I could make the receving page have no tags to start with because then I could simply write the remote response into the receiving page without creating double tags.
What's stopping you?  Parse the whole thing, and then have absolutely no HTML in your getresults page.  Since I would assume you're getting the entire response as a string, the way I've always done it, just take that string and Response.Write it to the page.  Since it's all HTML you won't have any connection to Viewstate, but I wouldn't think that would be a problem.
I'm not sure what kind of error you're getting since you wouldn't have any duplicate *server-side* tags, but if you use this method you won't have to worry about that.

Peter Brunone
MS MVP, ASP.NET
Founder, EasyListBox.com
Do the impossible, and go home early.
0
PeterBrunone
8/18/2005 1:30:35 AM
I think, the empty getresults.aspx has the page directive which should not be a problem, but it has html, body and form tags but no controls inside it. This is what is created by default for an aspx page in VS 2003. I guess I should try removing all tags in getresults.aspx, except for the page directive. May be that will solve my problem.

Anyways, I will try your suggestion tomorrow and see if it helps.

sun21170
0
sun21170
8/18/2005 1:41:46 AM

You should be able to remove all the HTML and not have it grow back... as long as you *never* open that page in Design View.
If Response.Write gives you trouble, add a single Literal to the HTML and put the content there instead.
If for whatever reason you can't do it this way, feel free to use my GetPiece function.  It's a bit wide (sorry about that), but it takes a string of content and gives you what's left between the two strings you specify (so feeding it myResult, "", and "" will give you the main content; then it's just up to you to do a few String.Replace operations).
Function GetPiece(ByVal strBody As String, ByVal strBegin As String, ByVal strEnd As String) As String
    Dim strResult As String = ""
    Try
        strResult = strBody.Substring((strBody.IndexOf(strBegin) + strBegin.Length), (strBody.IndexOf(strEnd) - (strBody.IndexOf(strBegin) + strBegin.Length)))
    Catch e As Exception
        If strResult = "" Then strResult = "error"
    Finally
    End Try
    Return strResult
End Function ' GetPiece

Peter Brunone
MS MVP, ASP.NET
Founder, EasyListBox.com
Do the impossible, and go home early.
0
PeterBrunone
8/18/2005 1:56:39 AM

I think that's a great function. I will definitely use that. Thanks.

sun21170
0
sun21170
8/18/2005 2:03:57 AM
It worked by removing all tags from the receiving page.

Thanks to everybody for their help.

sun21170
0
sun21170
8/21/2005 6:10:31 PM
Reply:
, ,, , , ,
tags?">

,

,

,

,

tags?&url=http://codeverge.com/asp.net.getting-started/how-to-strip-a-string-of-html-html-bo/665095', 350, 150); } else if (res.result == 'invalid') return; else if (res.result == 'ok') { parent.OpenModal('/fr~share?subject=How to strip a string of , ,,

,

,

,

tags?&url=http://codeverge.com/asp.net.getting-started/how-to-strip-a-string-of-html-html-bo/665095', 350, 150, function () { parent.location = res.url + '?refresh=1'; }); } else OpenModal('/fr~signin', 450, 350); }) .fail(function (x) { $.isLoading('hide'); alert(x.responseText); }) }