Tuesday, May 23, 2006

Code smells

Great article on coding rules on Coding horror blog.
Bottom line: Keep refactoring your code.

"Working clean means constant refactoring"

Sunday, May 21, 2006

Saving TWebbrowser content with IPersistFile

With TWebbrowser it is easy to navigate URL's and files on your local disk using the Navigate method.
As discussed in this blogpost TWebbrowser implements a IHTMLDocument2 interface object which can be used to edit the document in WYSIWYG way.

Saving the changes can be done in a few different ways, I will discuss a few here below:

1. Using the innerHTML property
You could retreive the raw HTML from the browser through the WebBrowser.OleObject.Document.documentElement.innerHTML property and then save it to disk.
If you want only the text from the document, and not the HTML, you could use the WebBrowser.OleObject.Document.documentElement.innerText method.
Saving to disk could be easily done using a TStringList object.

2. Through the IPersistFile interface
Although the above method works, there is a much more elegant way to achieve this.
IHTMLDocument implements an interface called IPersistFile which is an common interface for loading and saving objects.
Save the current loaded webpage using the IPersistFiles as follows:

(WebBrowser.Document as IPersistFile).Save('c:\inetpub\wwwroot\MyWebsite\index.html', True);

Besides Save, IPersistFile offers a method for loading files (Load) and a method to find out if the current loaded page is changed compared to when it was last saved. (IsDirty)

For more information on IPersistFile visit the msdn website here.
According this page IPersistFile is new in the .NET Framework 2.0.