Saturday 20 June 2009

Reading and Displaying an RSS2.0 Feed with C# and ASP.net

Recently I had to create a RSS feed for a website I had been asked to build (Smart Live Casino), and was asked to also provide a panel in the sidebar displaying the latest three feeds.

I'm not going to cover the creating of the XML feed itself here (there's tonnes of documentation on the website for this), instead I'm just going to explain how I ended up creating this:



In its simplest form, an RSS2.0 feed consists of the Rss Chanel (Chanel node), which contains the feed title, description and source ID, and a list of items that are in the feed:



For this I'm not really interested in the feed title, description or link, as I'm only displaying the items; so I'm only interested in displaying the title, publication date, description and title.

The first thing I did was create the basic control for each feed item. The markup is simply a header element containing a hyperlink to the item, the date and description, and another hyperlink telling the user that there's 'more':



Obviously this will just look a mess without the styling, so the stylesheet contains the elements:



And obviously I needed to add in some code behind to allow me to set the values:



And that's it! All that's needed for the control...next to nothing, if you ask me.

Next, I created the panel to hold the markup. There are several panels actually on the page (visit the SmartLiveCasino website to see what I mean) these are all hosted in a div with an ID of "sidebar" (because its the sidebar, funnily enough) - so when you see the CSS later, that's why everything is in #sidebar.

Anyway, the panel consists of two elements, a panelHeader, which contains the title, and a panelDetail, which contains the, well, panel detail. For my feed panel, the header simply contains a link to the Roulette advice hosted on the site. This is floated left, so that I can float the RSS feed image to the right, which itself contains a link to the Roulette Articles feed.

The panelDetail div is given an ID of rssFeed, and set to runat="server", so that we have access to the markup before its rendered.



Again, here's the CSS for the panel.



Obviously we're now up to the main bit of the panel...how to display the RSS feed. This is fairly simple, thanks to Linq! Before Linq, we'd be XPathing through the XML and jumping through hoops (well, not really, but it's far easier with Linq):



This code is very straightforward. First, we load the RSS feed into an XDocument (the rssFeedUrl and maximumRssPosts are set from the web.Config in the Page_Load method, and this is called just after).

Next, we use Linq to create an IEnumerable list of items in the feed. The Linq creates a new structure for each item it finds, and then will only 'Take' the top 'maximumRssPosts'.

Now we have a collection of the items, we simply cycle through them, creating a new control each time and adding it to the rssFeed. Once that's done, we do some error checking, and just stick a label into the empty box if there's been an error.

Yeah...okay, it's not too tidy, probably could be better (certainly there's a lack of error handling), but it works, so I don't care!

Since then, I've made the panel into a control in its own right (this is now on my Live TV Roulette site).

Sunday 17 May 2009

Virtual PC on Windows 7

One of the things I utilised fairly heavily on Vista was Virtual Server. Why Virtual Server rather than Virtual PC? Well, the main reason was the really annoying window that you had to use with Virtual PC which was either a non-regulation size, or didn't seem to work right in full screen. It also had to be kept open while the VM was running. This had been a problem in VPC for ever (I actually used to use the Connetix version of the product before MS had even thought publicliy about virtualisation).

With Virtual Server I could start a session, remote desktop into it to make my changes, and close the window. If I was doing some comms work, or checking out a remote client or server I'd written and wanted to, say, check encryption 'over the wire' or a remote deployment, I could just leave it running then connect into it through a RDC again later.

Installtion of Virtual Server 2007R2 SP1 isn't possible - Win7 isn't a supported product. However, I rely heavily on a VM of Windows XP I have with several different browsers installed to test one of my sites for my SuperCasino program (blatent plug!), so I had to install Virtual PC for Windows 7 Beta.

Installation was straightforward, and I was even able to use my existing VM, although I did uninstall the old additions and add the new ones when prompted by VPC. I've only had a cursory look at the moment but two things have really impressed me.

1) USB Support. This allows you to use and access your USB devices. A big, big bonus if, like me, you transfer a lot of large files like ISO images on a single removable disk.
2) Full screen mode is now available as a RDC!!! That's the killer app for me; true full screen and no worries about the 'spare' window I hated hanging around.

So I'm not too bothered about VS2007R2SP1 now - it looks like VPC will fill my needs completely.

Friday 15 May 2009

First Windows 7 & Asus W2P 'anomaly'.

Found my first really weird A2P and Windows 7 bug today...

It seems that if Win7 has gone into screen sleep mode (where your monitor or LCD display has turned off), then when it awakes the screen is in 'low power' mode. That is, it's very dim.

Attempting to use the ATK keys no longer work either - it's impossible to brighten the screen using them. A quick check on the power options (where it's possible to brighten or dim the screen) shows that brightness is at full. Attempting to reset the brightness using this (i.e. set everything to dim, then full) has no effect whatsoever.

The W2P screen seems dim by normal laptop standards anyway (at least to my eyes, and in comparison with other laptops I've had - it's probably down to the huge screen and big resolution anyway), so a dim screen is certainly no benefit! It's atrociously bad; almost impossible to read, when dim.

None of the other keys work such as raising/lowering volume, swapping display, turning bluetooth on/off, so this may not be completely a Win7 problem, perhaps an Asus ATK issue.

Asus is pretty bad on support - there's been no updated drivers since 2007, so for things like displays (which are now a full version behind the published ATI drivers), you're on your own. I don't expect a resolution to this.

Still, if there's an option in Windows to increase/decrease screen illumination, I would have expected it to work.

The only resolution seems to be a reboot. Fortunately, Win7 reboots around three times as fast as Vista (at least, at the moment). Let's see what happens when more cruft is added.

Thursday 14 May 2009

Windows 7 Installation on Asus W2P part 2 - Software

Now that I'd got a Win7 installation I was happy with (well, sort of - I can't seem to totally get rid of fuzzytype or not-very-clear-type), it was time to install the software.

Typically, the first thing I install is all the browser bits'n'bobs that I seem to be needed - Java, Flash, Sliverlight, etc. So off I went to the relevant sites to see if any had Windows 7 versions. None did, so I just installed the defaults; they seemed to work okay.

Next I installed Office 2007 Ultimate, Visio, and Project. The only issue here was the Office Ultimate key wouldn't validate. Odd; I was using the same key that was on the laptop previously, and was under the impression that it should authenticate okay if the hardware hadn't changed (which it hadn't). One quick call to Microsoft though, and it activated okay - no doubt I'll have to do the same again when the full version of Win7 comes out. No such problems with either Visio or Project, though.

Office 2007 worked fine, except for its insistance on using Cleartype (you may have guessed by now that I hate cleartype). The option for turning it off no longer appeared either in the settings tab; so I'll have to live with that.

On the subject of Cleartype, why can't I turn it totally off? I hate having things like this forced on me. I think I read somewhere that 1 in 10 people get the blurry effect like I do, but whenever I look at any MS groups or KB articles, the (rather arrogant) answer seems to be "studies have found people like cleartype, so we're keeping it. And anyway, we can't reproduce the problem [of fuzzy text]". It's strange that so many people seem to post about not liking it, yet MS seem to refuse to acknowledge that not everyone either likes it or finds it makes screens easier to read.

Next came Expression Studio - this installed fine (except for the clearty...oh, you get the idea), and activated okay.

After this I installed VSTS2008, XNA3, TFS Client, and then VS2008SP1. During my installation I chose not to include SQL Express 2005, as I am also installing SQL Server Developer 2008 after installing VS2008. This all installed smoothly and worked fine.

Finally, I installed SQL Server 2008 - the only issue I had here was the number of "This software has known problems" message that kept appearing, however it installed fine, and then the upgrade to SP1 fixed any outstanding issues. MSDN Library and and updated SQL Books Online were finally put on my machine.

All the software I initially need to work was now on my machine; I went to Windows Update and installed the 67(!) updates required.

Finally, I rebuilt a DB from SQL Server 2005 scripts on my SQL Server 2008 installation, and compiled a website I was working on. All worked fine.

So that's the basic install and development system up and running. Next I'm going to look at installing many of the tools I also use during development, and Nero (which should be interesting).

Wednesday 13 May 2009

Windows 7 installation on Asus W2P

Windows 7 RC has been out for a couple of weeks now, and I finally got around to installing it on a development laptop (Asus W2P) yesterday. Installation went fairly easily; my HD is partitioned into two drives; the primary partition (C Drive) being used for software, and the secondary (D Drive) is used for data.

I decided to go for a clean install; simply formatting the the C drive and installing Win7 there - it's not that I don't trust the 'upgrade' option; I just feel that with a fresh install I'll remove the cruft thats accumilated since my last OS install.

The basic OS install took around an hour. It was largely just waiting, but yet again, the install operation stops around half way through to ask questions. As the machine was at this point not connected to my network, I created a temporary user name to log on with. Rather annoyingly, this username was being used for all subsequent software installations (a quick hack around with regedit solved that little problem).

After logging on, the first thing I noticed was the new taskbar. Rather than containing a group of quick links like in XP and Vista which then open up in the taskbar, these almost-like-but-not-actually-quicklinks simply highlight once the application has been opened. Hovering over the icon brings up a preview of each opened window for this application. Rather suprisingly, although it looked alien at first, I now quite like this feature.

Next, I installed my bluetooth mouse vua the control panel (very straightforward), and then installed the rest of the applications I need for general day-to-day use (things like IIS, MSMQ, etc). This took around 15-20 minutes, and once done, I ran windows update.

WU picked up eighteen updates, which I then installed and rebooted from. After reboot, I was pleased to see that windows now had correct drivers for everything with the exception of one device (the TV tuner). I also noticed that my synaptics touchpad was using a generic driver, so I couldn't turn off the annoying click-tap feature, and none of the hotkeys worked.

One quick trip to the Asus website later, and the touchpad, hotkeys, and TV tuner were all plugged in and working fine. I simply downloaded the latest Vista versions, and everything was fine.

The OS was now up and running. All that I had to do to get the OS to a state where I was happy with it was turn off the annoyingly blurry "cleartype" (which actually doesn't seem to turn it off everywhere), stop the "smoothing of screen fonts" which isn't cleartype, but produces the same blurry effect, and then install the rest of my software...of which, more later.

Friday 19 December 2008

Daves Spare Brain

This is a blog where I'm hoping to store all the stuff that I learn and then forget about.

As an aside, I'm an IT consultant, who's either an analyst and/or developer and/or designer and/or not-as-technical-as-people-seem-to-think-I-am architect and/or not-as-knowledgeable-about-solutions-as-people-seem-to-think-I-am designer (delete as appropriate) depending on the job I'm doing and the client that I'm working for. Most of the stuff dumped out of my brain here will be related to the .net framework, mainly C# and SQL Server. I've been working in IT for 18 years now, the last seven have been mainly MS technologies (COM/C++/VB/etc), with a bit of Unix and Java thrown in for good measure.

I'm proud to say I haven't used TSO in over eight years, and can barely remember what MVS stands for!