If you are like me you probably have a hard time finding something to create when trying out new stuff. This time I sat down and tried to figure out something to use LINQ and Silverlight for and maybe WCF, WPF and Ajax as well. At the time I was watching TV and the EPG (Electronic Programming Guide) didn’t work, so I started to look for a web site with a program guide supporting reminders and other nice features to let you know what’s on TV. I didn’t find anything good.
I then started to look for an xml/text format for TV programs. I did some searching and came across
xmltv. Nice! I now had an xml source looking something like this:
<tv>
<channel id="channel1">
<display-name lang="nb">Channel 1</display-name>
</channel>
<channel id="channel2">
<display-name lang="en">Channel 2</display-name>
</channel>
...
<programme start="20070801033000 +0200" stop="20070801053000 +0200" channel="channel1">
<title lang="nb">Program 1</title>
<desc lang="nb">Program description.</desc>
<credits>
<director>...</director>
<actor>...</actor>
<actor>...</actor>
</credits>
<date>2004</date>
<category lang="en">movie</category>
<category lang="en">drama</category>
</programme>
...
</tv>So now I had an xml document containing all the data I needed
and this was a perfect time to try out XLINQ. I decided to stick with the LINQ
query syntax and not mix in Lambda expressions to keep things simple and
readable. I came up with this expression to get all TV channels:
public List<TvChannel> GetTvChannelsWithoutGuide()
{
IEnumerable<TvChannel> channels;
try
{
XElement tvGuide = XElement.Load(_xmlFileLoc);
channels = from c in tvGuide.Descendants("channel")
orderby (string)c.Element("display-name") ascending
select new TvChannel
{
Id = (string)c.Attribute("id"),
Name = (string)c.Element("display-name"),
Lang = (string)c.Element("display-name").Attribute("lang"),
TvGuideLoaded = false
};
}
catch (FileNotFoundException fileNotFoundEx)
{
throw new FileNotFoundException(string.Format("Xml file for tv guide not found at {0}",
_xmlFileLoc), fileNotFoundEx);
}
return channels.ToList<TvChannel>();
}
The XElement gives me the complete xml document that I can
use XLINQ expressions on. In my query I’m looking only for TV channels, so I
specify that in the query by
from c in tvGuide.Descendants("channel")This gives my a variable c that I use in my orderby clause and select statement. The orderby clause
orderby (string)c.Element("display-name") ascendinguses the c variable to access the “display-name” element to set order by. But it’s the select statement which is the cool thing here! First here a listing of the TvChannel class:
[Serializable]
public class TvChannel
{
public string Id { get; set; }
public string Name { get; set; }
public string Lang { get; set; }
[XmlIgnore]
public IEnumerable<TvProgramme> TvGuideNonSerializable {
get { return TvGuide; }
set { TvGuide = value.ToList(); }
}
public List<TvProgramme> TvGuide
{
get;
set;
}
public bool TvGuideLoaded { get; set; }
}
Ignore the TvGuideNonSerializable property for now. By using
select new TvChannel I can create an object of my TvChannel class an assign data from the xml document to my properties. I just love this syntax! It’s really nice and easally understandable. Another cool feature (running VS 2008 Beta 2) is that you get ItelliSense in LINQ for your classes, like this:

I also created other methods for retrieving all programs for one channel, all channels etc. At the time of writing I’m working on a Silverlight implementation for the TvGuide which I hopefully can show you soon. And later I’ll make all source code available.