Help in String Manipulation

sunilshishodia

New member
Joined
Jul 18, 2006
Messages
3
Programming Experience
1-3
I am having a string that contains HTML. I would like to replace some parts of the strings with some other content.For example: If my string contains
HTML:
<a id="_ctl4_dlUserPortfolio__ctl1_lnkBtnCategory" href="{if (typeof(Page_ClientValidate) != 'function' || Page_ClientValidate()) __doPostBack('_ctl4$dlUserPortfolio$_ctl1$lnkBtnCategory','')} " style="color:#000000">Academics</a>
<a id="_ctl4_dlUserPortfolio__ctl4_lnkBtnCategory" href="{if (typeof(Page_ClientValidate) != 'function' || Page_ClientValidate()) __doPostBack('_ctl4$dlUserPortfolio$_ctl4$lnkBtnCategory','')} " style="color:#000000">Case Study</a>
I want to change href of both to Academics.html and Case Study.html respectively. How can I change them in single string manipulation. If single is not possible then, how to do it by loop.
 
Last edited by a moderator:
An example using the HtmlDocument in WebBrowser to parse and replace attributes:
VB.NET:
Dim wb As New WebBrowser
wb.Navigate("about:blank")
wb.Document.Write(your_html_string)
For Each he As HtmlElement In wb.Document.GetElementsByTagName("a")
  he.SetAttribute("href", he.InnerText & ".html")
Next
MsgBox(wb.Document.Body.OuterHtml)
 
Thanks for reply. I am getting HTML source of a page and then assigning it to a string variable. like Dim strHtml as string =
"<a id=""_ctl4_dlUserPortfolio__ctl1_lnkBtnCategory"" href={if (typeof(Page_ClientValidate) != 'function' || Page_ClientValidate()) __doPostBack('_ctl4$dlUserPortfolio$_ctl1$lnkBtnCategory','')} "" style=""color:#000000"">Academics</a>"

My string can have many number of above contained in it. Can i use your code for string also?
 
Yes, that was the point of the "For Each a-tag ..."
 
Do I have to include any namespace for using WebBrowser class. I am using VS 2003. Moreover, I would like to make permanent changes to the string by changing its href. Does this code make manipulations in string ? I will be saving the manipulated string at client machine so that after my pages are downloaded, clicking on link button works HTML way.
 
Your request was VS2005/.Net 2.0 as stated by your Primary Platform. If .Net 2.0 is not your primary platform you must update your user profile. If only this request was for a different version you must take care to specify what platform your inquiry is for.

.Net 2.0 Webbrowser class didn't exist in .Net 1, but there exist an alternative in the COM library MSHTML.

Add Reference to "Microsoft Html Object Library" in COM page of dialog. Then use this code instead:
VB.NET:
Dim html As New mshtml.HTMLDocument
Dim doc As mshtml.IHTMLDocument2 = html
doc.write(your_html_string)
doc.close()
html = doc
For Each ihe As mshtml.IHTMLElement In html.getElementsByTagName("a")
  ihe.setAttribute("href", ihe.innerText & ".html")
Next
MsgBox(html.body.outerHTML)
 
Back
Top