VB Orcas RSS Feeder example

Orcas Beta 1 samples include a RSS feeder for C#, but there isn’t one for VB, so I thought I’d slap one together.  Some things to note compared to the C# one is the use of XML literals an expressions.  Also note the use of LINQ expressions rather than using yield and custom enumerators.. the result is the same in terms of delayed evaluation even though this example doesn’t actually show that ;)

 

Option Strict On : Option Explicit On : Option Infer On
 
Imports System
Imports System.Collections.Generic
Imports System.Linq
Imports System.Xml
Imports System.Xml.Linq
 
 
Module Module1
 
   Const feedURL As String = “http://+:8086/vbfeeds/”
   Const feedURI As String = “http://localhost:8086/vbfeeds/”
 
   Function GetItems() As IEnumerable(Of XElement)
      Dim feeds() As String = {“http://msmvps.com/blogs/bill/rss.aspx”, _
      “http://blogs.msdn.com/vbteam/rss.aspx”, _
     “http://www.panopticoncentral.net/Rss.aspx”, _
      “http://blogs.msdn.com/xmlteam/rss.aspx”, _
      “http://blogs.msdn.com/aconrad/rss.aspx”}
 
      Return From feed In feeds _
         From item In XDocument.Load(feed).Root.<channel>.<item> _
         Select item
 
   End Function
 
 
   Function GetReplyBody() As XElement
      Return <rss version=“2.0”>
               <channel>
                  <title>VB Geeks</title>
                  <link><%= feedURL %></link>
                  <description>VB bloggers</description>
                  <generator>XLinq-based RSS aggregator</generator>
                  <%= GetItems.ToArray %>
               </channel>
            </rss>
   End Function
 
 
   Sub Main()
      Dim listener As New Net.HttpListener()
      Console.WriteLine(“starting server”)
      listener.Prefixes.Add(feedURL)
      listener.Start()
 
      Console.WriteLine(“opening browser pointed at this server”)
      Diagnostics.Process.Start(“iexplore.exe”, feedURI)
 
      While True
         Dim response = listener.GetContext.Response
         response.ContentType = “text/xml”
         Using writer As New XmlTextWriter(response.OutputStream, Text.Encoding.UTF8)
            GetReplyBody.WriteTo(writer)
         End Using
      End While
 
   End Sub
 
 
End Module

 

 

‘ for those interested this is the original C# source :

 

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Xml;
using System.Linq;
using System.Xml.Linq;
using System.Net;
 
// See the ReadMe.html for additional information
class app
{
   const string feedUrl = “http://+:8086/csharpfeeds/”;
 
   static IEnumerable<XElement> GetItems()
   {
      string[] feeds = {
            “http://blogs.msdn.com/ericlippert/rss.aspx”,
            “http://blogs.msdn.com/wesdyer/rss.aspx”,
            “http://blogs.msdn.com/charlie/rss.aspx”,            
            “http://blogs.msdn.com/cyrusn/rss.aspx”,
            “http://blogs.msdn.com/mattwar/rss.aspx”,
            “http://blogs.msdn.com/lucabol/rss.aspx”,
            “http://www.pluralsight.com/blogs/dbox/rss.aspx”,
            “http://blogs.msdn.com/jomo_fisher/rss.aspx”
        };
      foreach (var str in feeds)
      {
         var feed = XDocument.Load(str);
         var items = feed.Root.Element(“channel”).Elements(“item”);
         foreach (var item in items)
            yield return item;
      }
   }
 
   static XElement GetReplyBody()
   {
      return new XElement(“rss”,
         new XAttribute(“version”“2.0”),
         new XElement(“channel”,
            new XElement(“title”“C# Geeks”),
            new XElement(“link”, feedUrl),
            new XElement(“description”“C# Team Members”),
            new XElement(“generator”“XLinq-based RSS aggregator”),
            GetItems().ToArray()
            ));
 
   }
 
   static void Main()
   {
      var listener = new HttpListener();
 
      listener.Prefixes.Add(“http://+:8086/csharpfeeds/”);
      listener.Start();
 
      // Open a browser pointing at the feeds being served.
      string uri = @”http://localhost:8086/csharpfeeds/”;
      System.Diagnostics.Process browser = new System.Diagnostics.Process();
      browser.StartInfo.FileName = “iexplore.exe”;
      browser.StartInfo.Arguments = uri;
      browser.Start();
 
      // Serve requests.
      while (true)
      {
         var context = listener.GetContext();
         var body = GetReplyBody();
         context.Response.ContentType = “text/xml”;
         using (XmlWriter writer = new XmlTextWriter(context.Response.OutputStream, Encoding.UTF8))
            body.WriteTo(writer);
 
      }
   }
}

 

 

 


This entry was posted in 2087, 2088, 491. Bookmark the permalink.

One Response to VB Orcas RSS Feeder example

  1. Nick says:

    Great conversion. I love that VB has all these cryptic options now. I just hope that Orcas will default them all to On at project level (for new projects at least)

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>