RssFeed XML literals sample

Download this source code


Option Strict On
Option Explicit On
Option Infer On

Imports Microsoft.VisualBasic
Imports System.Runtime.CompilerServices
Imports <xmlns:dc="http://purl.org/dc/elements/1.1/">
Imports <xmlns:slash="http://purl.org/rss/1.0/modules/slash/">
Imports <xmlns:wfw="http://wellformedweb.org/CommentAPI/">

''' <summary>
''' Represents a category for an RSS item.
''' </summary>
Public Class RssCategory
    Private _name As String
    Private _domain As String

    ''' <summary>
    ''' Gets or sets the name of the category.
    ''' </summary>
    ''' <value>The name of the category.</value>
    Public Property Name() As String
        Get
            Return _name
        End Get
        Set(ByVal value As String)
            _name = value
        End Set
    End Property

    ''' <summary>
    ''' Gets or sets the domain of the category.
    ''' </summary>
    ''' <value>The domain of the category, usually a URL.</value>
    Public Property Domain() As String
        Get
            Return _domain
        End Get
        Set(ByVal value As String)
            _domain = value
        End Set
    End Property

End Class

''' <summary>
''' Represents an item in an RSS feed.
''' </summary>
Public Class RssItem
    Private _title As String
    Private _link As String
    Private _guid As String
    Private _guidIsPermalink As Boolean
    Private _pubDate As Date
    Private _creator As String
    Private _description As String
    Private _commentCount As Integer?
    Private _commentsLink As String
    Private _commentRssUrl As String
    Private _categories As IEnumerable(Of RssCategory)

    ''' <summary>
    ''' Gets or sets the title of the item.
    ''' </summary>
    ''' <value>The title of the item.</value>
    Public Property Title() As String
        Get
            Return _title
        End Get
        Set(ByVal value As String)
            _title = value
        End Set
    End Property

    ''' <summary>
    ''' Gets or sets the link of the item.
    ''' </summary>
    ''' <value>The URL where the item can be found.</value>
    Public Property Link() As String
        Get
            Return _link
        End Get
        Set(ByVal value As String)
            _link = value
        End Set
    End Property

    ''' <summary>
    ''' Gets or sets a globally unique identifier for the item.
    ''' </summary>
    ''' <value>A string that uniquely identifies the item. Usually a URL.</value>
    Public Property Guid() As String
        Get
            Return _guid
        End Get
        Set(ByVal value As String)
            _guid = value
        End Set
    End Property

    ''' <summary>
    ''' Gets or sets a value that indicates whether the value of the <see cref="Guid" /> property
    ''' is a permalink for the item.
    ''' </summary>
    ''' <value>True if the value of the <see cref="Guid" /> property is a permalink; otherwise, false.</value>
    Public Property GuidIsPermalink() As Boolean
        Get
            Return _guidIsPermalink
        End Get
        Set(ByVal value As Boolean)
            _guidIsPermalink = value
        End Set
    End Property

    ''' <summary>
    ''' Gets or sets the date at which the item was published.
    ''' </summary>
    ''' <value>A <see cref="DateTime" /> that indicates when the item was published.</value>
    Public Property PubDate() As Date
        Get
            Return _pubDate
        End Get
        Set(ByVal value As Date)
            _pubDate = value
        End Set
    End Property

    ''' <summary>
    ''' Gets or sets the creator or author of the item.
    ''' </summary>
    ''' <value>The name of the creator or author of the item.</value>
    Public Property Creator() As String
        Get
            Return _creator
        End Get
        Set(ByVal value As String)
            _creator = value
        End Set
    End Property

    ''' <summary>
    ''' Gets or sets the description of the item.
    ''' </summary>
    ''' <value>A description of the item, usually in HTML format.</value>
    Public Property Description() As String
        Get
            Return _description
        End Get
        Set(ByVal value As String)
            _description = value
        End Set
    End Property

    ''' <summary>
    ''' Gets or sets the number of comments for this item.
    ''' </summary>
    ''' <value>The number of comments, or <see langword="null" /> if it the item cannot have comments.</value>
    Public Property CommentCount() As Integer?
        Get
            Return _commentCount
        End Get
        Set(ByVal value As Integer?)
            _commentCount = value
        End Set
    End Property

    ''' <summary>
    ''' Gets or sets a link to a page containing the comments for this item.
    ''' </summary>
    ''' <value>A URL that links to a page containing the comments for this item, or <see langword="null" /> if
    ''' this item does not have comments.</value>
    Public Property CommentsLink() As String
        Get
            Return _commentsLink
        End Get
        Set(ByVal value As String)
            _commentsLink = value
        End Set
    End Property

    ''' <summary>
    ''' Gets or sets a link to an RSS feed containing the comments for this item.
    ''' </summary>
    ''' <value>A URL that links to an RSS feed containing the comments for this item, or <see langword="null" /> if
    ''' this item does not have comments.</value>
    Public Property CommentRssUrl() As String
        Get
            Return _commentRssUrl
        End Get
        Set(ByVal value As String)
            _commentRssUrl = value
        End Set
    End Property

    ''' <summary>
    ''' Gets or sets the categories that this item belongs to.
    ''' </summary>
    ''' <value>A list of <see cref="RssCategory" /> objects that indicate the categories that this item belongs to.</value>
    Public Property Categories() As IEnumerable(Of RssCategory)
        Get
            Return _categories
        End Get
        Set(ByVal value As IEnumerable(Of RssCategory))
            _categories = value
        End Set
    End Property

End Class

''' <summary>
''' Represents an RSS feed channel.
''' </summary>
Public Class RssFeed
    Private _title As String
    Private _link As String
    Private _language As String
    Private _items As IEnumerable(Of RssItem)

    ''' <summary>
    ''' Gets or sets the title of the RSS feed channel.
    ''' </summary>
    ''' <value>The title of the channel.</value>
    Public Property Title() As String
        Get
            Return _title
        End Get
        Set(ByVal value As String)
            _title = value
        End Set
    End Property

    ''' <summary>
    ''' Gets or sets the link to the HTML website corresponding to the RSS feed channel.
    ''' </summary>
    ''' <value>A URL to a website corresponding to the channel.</value>
    Public Property Link() As String
        Get
            Return _link
        End Get
        Set(ByVal value As String)
            _link = value
        End Set
    End Property

    ''' <summary>
    ''' Gets or sets the language of the RSS feed.
    ''' </summary>
    ''' <value>The two-letter ISO code of the language of the content, or <see langword="null" /> if none is specified.</value>
    Public Property Language() As String
        Get
            Return _language
        End Get
        Set(ByVal value As String)
            _language = value
        End Set
    End Property

    ''' <summary>
    ''' Gets or sets the items of the channel.
    ''' </summary>
    ''' <value>A list of <see cref="RssItem" /> objects that represent the items of the channel.</value>
    Public Property Items() As IEnumerable(Of RssItem)
        Get
            Return _items
        End Get
        Set(ByVal value As IEnumerable(Of RssItem))
            _items = value
        End Set
    End Property

    ''' <summary>
    ''' Creates an instance of the <see cref="RssFeed" /> class based on an existing XML RSS feed.
    ''' </summary>
    ''' <param name="feed">The <see cref="XDocument" /> for the feed.</param>
    ''' <returns>A <see cref="RssFeed" /> object representing the XML RSS feed.</returns>
    Public Shared Function FromXml(ByVal feed As XDocument) As RssFeed
        If feed Is Nothing Then
            Throw New ArgumentNullException("feed")
        End If

        Dim result = From channel In feed.<rss>.<channel> _
                     Select New RssFeed() With _
                         { _
                             .Title = channel.<title>.Value, _
                             .Link = channel.<link>.Value, _
                             .Language = channel.<dc:language>.Value, _
                             .Items = From item In channel.<item> _
                                      Select New RssItem() With _
                                          { _
                                              .Title = item.<title>.Value, _
                                              .Link = item.<link>.Value, _
                                              .Guid = item.<guid>.Value, _
                                              .GuidIsPermalink = (item.<guid>.@isPermaLink = "true"), _
                                              .PubDate = Date.Parse(item.<pubDate>.Value), _
                                              .CommentCount = CType(item.<slash:comments>.Value, Integer?), _
                                              .CommentsLink = item.<comments>.Value, _
                                              .CommentRssUrl = item.<wfw:commentRss>.Value, _
                                              .Description = item.<description>.Value, _
                                              .Categories = From category In item.<category> _
                                                            Select New RssCategory() With _
                                                                { _
                                                                    .Name = category.Value, _
                                                                    .Domain = category.@domain _
                                                                } _
                                          } _
                         }

        Return result.First()
    End Function


    ''' <summary>
    ''' Creates an XML document in RSS 2.0 format for this RSS feed.
    ''' </summary>
    ''' <returns>A <see cref="XDocument" /> for the RSS feed.</returns>
    Public Function CreateXml() As XDocument
        Dim itemElements = From item In Items _
                           Select <item>
                                      <title><%= item.Title %></title>
                                      <link><%= item.Link %></link>
                                      <guid isPermaLink=<%= item.GuidIsPermalink.ToString().ToLowerInvariant() %>><%= item.Guid %></guid>
                                      <pubDate><%= item.PubDate.ToString("r") %></pubDate>
                                      <dc:creator><%= item.Creator %></dc:creator>
                                      <%= CreateCommentCountElement(item.CommentCount) %>
                                      <%= CreateCommentsElement(item.CommentsLink) %>
                                      <%= CreateCommentRssElement(item.CommentRssUrl) %>
                                      <description><%= New XCData(item.Description) %></description>
                                      <%= CreateCategories(item.Categories) %>
                                  </item>

        Return <?xml version="1.0" encoding="utf-8"?>
               <rss version="2.0">
                   <channel>
                       <title><%= Title %></title>
                       <link><%= Link %></link>
                       <dc:language><%= Language %></dc:language>
                       <%= itemElements %>
                   </channel>
               </rss>
    End Function

    Private Function CreateCommentCountElement(ByVal commentCount As Integer?) As XElement
        If commentCount Is Nothing Then
            Return Nothing
        Else
            Return <slash:comments><%= commentCount %></slash:comments>
        End If
    End Function

    Private Function CreateCommentsElement(ByVal commentLink As String) As XElement
        If commentLink Is Nothing Then
            Return Nothing
        Else
            Return <comments><%= commentLink %></comments>
        End If
    End Function

    Private Shared Function CreateCommentRssElement(ByVal commentRssUrl As String) As XElement
        If commentRssUrl Is Nothing Then
            Return Nothing
        Else
            Return <wfw:commentRss><%= commentRssUrl %></wfw:commentRss>
        End If
    End Function

    Private Function CreateCategories(ByVal categories As IEnumerable(Of RssCategory)) As IEnumerable(Of XElement)
        If categories Is Nothing Then
            Return Nothing
        Else
            Return From category In categories _
                   Select <category domain=<%= category.Domain %>><%= category.Name %></category>
        End If
    End Function
End Class