I needed this functionality today and found a great blog post about it.
The power of XSL truly comes to surface in these webparts.
For example, the DataFormWebPart needs a datasource to be configured. Doing this in code is about the same as copying the aspx from SharePoint Designer.
I would suggest you build up your web part in SPD and then – in code – mimic every property set by SPD.
Well, not every property, as it still uses deprecated properties.
Most importantly, use the XslLink instead of hard coding the XSL node into your code. With the XslLink, you just supply an URL (for example in your Styles mapped folder) which can be altered at run time by an administrator, not the end users.
The post also elaborates on how to set the data source, but looking at how SPD does it, mostly gives way easily.
Oh and in case you’re wondering why in heaven’s name I would do this in code is mainly because one of the webparts I use is a ListViewWebPart and its ListID is hard coded into it by SPD and is therefore not reusable for redeployment.
Edit: I stumbled upon Jan Tielens’ blog post first, but found only basic guidelines to my problem, and it still used the now deprecated SPWebPartCollection approach.