Contents tagged with XML

  • A Templated ASP.NET RSS Feed Reader Control

    Update 20070330:

    By popular demand (well a couple people) I have included a sample project with everything needed to get started using this control.

    Basically just a sample web project, click here to get the zipped archive. You might notice things are done a little different, I include the System.ServiceModel.Syndication namespace on my page so that I can case the Container.DataItem to a SyndicationItem and access its properties that way, stops using Eval, but each to their own.


    Hope that makes it easier for some, will be including full source in future too :).

    Project Files:
    TemplatedRSSFeedReader.zip

    Thanks
    Stefan


    Update 20070319:

    Today I attended the Heros Happen event in Perth, and I learnt something very very cool, you see how I wrote my own classes and code to load the syndicated feed in, then used LINQ to XML to load that into my classes. During Dave Glovers presentation on WCF he had a sample in there that was building an RSS feed, and I could see he was using some classes to do so. What I then noticed is these classes are new in .NET 3.5 and will make creation of this control 100 times easier plug give us full support of the RSS 2.0 spec and allow us to access all feed proprties :) :) and will mean *removing* code and making things much simpler.

    Firstly we need to add a reference to the System.ServiceModel.Web assembley, this is where the magic is. Then add a using System.ServiceModel.Syndication; to the RSSReader.cs class, we then have access to these helper classes which allow us to easily read and create RSS 2.0 and ATOM 1.0 feeds, as I am only supporting RSS I will only use the RSS formatter. To read our feed we need to do the below:

    Firslty read the feed into an XMLReader:

    XmlReader reader = XmlReader.Create("http://weblogs.asp.net/stefansedich/rss.aspx");

    Then create an instance of the RSS20FeedFormatter and then make this read the data from the xmlreader:

    Rss20FeedFormatter feedFormatter = new Rss20FeedFormatter();
    feedFormatter.ReadFrom(reader);


    We now can get access to the feed by using feedFormatter.Feed, this gives us an instance of a SyndicationFeed class which gives us access to all items and properties of our feed. The only difference now is that as some properties are a little different accessing them is different.So instead of feed.Title you use feed.Title.Text instead. The good thing with this is, 1. We have removed the need to do this manually and 2. We have all properties that belong to an RSS feed.

    The updated RSSReader class is below with the code changes. You can now remove the Feed and FeedItem classes we created earlier as they are needed no more. It just goes to show we learn something new every day.....


    Code:

  • I Think I Love LINQ

    I am beggining to really like to ease of using LINQ, On a project I am working on now I am storing configuration in an XML file like so:

    <sites>
        <site id="1" sitename="foo">
           <domains>
              <domain url="www.foo.com" />
           </domains>
           <settings>
              <setting key="foo" value="foo" />
           </settings>
        </site>
    </sites>

    My original solution to load this involded looping over the nodes looping over the domains and settings and loading this into my Site object. Time consuming to write and looked messy. And then LINQ to XML steps in. My code to parse this XML file into my collection of Site objects now becomes:

    Me.Sites = (From site In document.Descendants("site") _
                            Select New Site With { _
                            .ID = site.Attribute("id").Value, _
                            .SiteName = site.Attribute("sitename").Value, _
                            .Domains = (From domain In site.Elements("domains").Descendants("domain") _
                                        Select domain) _
                                        .ToDictionary(Function(domain) domain.Attribute("url").Value, Function(domain) domain.Attribute("url").Value), _
                            .Settings = (From setting In site.Elements("settings").Descendants("setting") _
                                        Select setting) _
                                        .ToDictionary(Function(setting) setting.Attribute("key").Value, Function(setting) setting.Attribute("value").Value) _
                            }).ToList()



    The speed of this is actually pretty fast and I tested with some large XML files and was happy with the performance. And I dont think life can get easier....


    Cheers