If you are looking to make permanent changes to your HTML file, first manage your HTML parsing by loading it into a System.Windows.Forms.WebBrowser
control. From there you can perform DOM-like modifications to the HTML without the dangerous repercussions of parsing corruption that can be caused by performing Regex.Replace
on the raw file. (Apparently RegEx + HTML is a serious issue for some).
So first in your code you would:
WebBrowser myBrowser = new WebBrowser();
myBrowser.URL = @"C:\MyPath\MyFile.HTML";
HtmlElement myDocBody = myBrowser.Document.Body;
Then you can navigate through your document body, seeking out your div
tag and looking for your anchor tags by using the HtmlElement.Id
property and HtmlElement.GetAttribute
method.
Note: feel free to still use RegEx matching on the URL strings but only after extracting them from a GetAttribute("href")
method.
To add the onClick
method, simply invoke the HtmlElement.SetAttribute
method.
When you have finished all your modifications, save the changes by writing the WebBrowser.DocumentText
to file.
Here is a reference:
http://msdn.microsoft.com/en-us/library/system.windows.forms.htmlelement.aspx