Home
About
Contact
Sunday, August 19, 2007
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") ascending


uses 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:

LinqIntellisence.png

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.

.Net | CSharp | Linq | TvGuide | VisualStudio
Sunday, August 19, 2007 1:11:51 PM (W. Europe Daylight Time, UTC+02:00)
RSS RSS - Comments Twitter LinkedIn
         
SEARCH
 
 
         
TOP POSTS
   
         
NAVIGATION
   
         
CATEGORIES
  .Net (61) ADFS (3) Agile (30) Ajax (5) Architecture (20) Articles (1) ASP.NET (6) ASP.NET-MVC (1) Blogging (12) Books (2) BPEL (1) CleanCode (1) CloudComputing (7) Community (4) CSharp (11) DasBlog (5) Database (2) DDD (5) Deployment (16) DSL (1) Events (38) ExtremeProgramming (6) Fun (6) Gadgets (4) IIS (10) InfoQ (4) Java (2) Lean (3) Linq (2) MemoryLeaks (5) Microsoft (37) MVC (1) NDC (2) NNUG (36) Other (10) Patterns (9) Performance (3) Scrum (17) Security (7) ServiceBus (1) Silverlight (4) Software (19) TeamManagement (11) TechEd (7) Testing (4) Tools (25) TvGuide (1) WCF (8) Web (15) WebDeploy (1) WIF (3) Windows (10) Vista (15) VisualStudio (16) WiX (9) Work (16) Workflow (3)  
         
ARCHIVE
   
         
BLOGROLL
   
         
ON THIS PAGE...
 
TV Program Guide - LINQ