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/">
Public Class RssCategory
Private _name As String
Private _domain As String
Public Property Name() As String
Get
Return _name
End Get
Set(ByVal value As String)
_name = value
End Set
End Property
Public Property Domain() As String
Get
Return _domain
End Get
Set(ByVal value As String)
_domain = value
End Set
End Property
End Class
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)
Public Property Title() As String
Get
Return _title
End Get
Set(ByVal value As String)
_title = value
End Set
End Property
Public Property Link() As String
Get
Return _link
End Get
Set(ByVal value As String)
_link = value
End Set
End Property
Public Property Guid() As String
Get
Return _guid
End Get
Set(ByVal value As String)
_guid = value
End Set
End Property
Public Property GuidIsPermalink() As Boolean
Get
Return _guidIsPermalink
End Get
Set(ByVal value As Boolean)
_guidIsPermalink = value
End Set
End Property
Public Property PubDate() As Date
Get
Return _pubDate
End Get
Set(ByVal value As Date)
_pubDate = value
End Set
End Property
Public Property Creator() As String
Get
Return _creator
End Get
Set(ByVal value As String)
_creator = value
End Set
End Property
Public Property Description() As String
Get
Return _description
End Get
Set(ByVal value As String)
_description = value
End Set
End Property
Public Property CommentCount() As Integer?
Get
Return _commentCount
End Get
Set(ByVal value As Integer?)
_commentCount = value
End Set
End Property
Public Property CommentsLink() As String
Get
Return _commentsLink
End Get
Set(ByVal value As String)
_commentsLink = value
End Set
End Property
Public Property CommentRssUrl() As String
Get
Return _commentRssUrl
End Get
Set(ByVal value As String)
_commentRssUrl = value
End Set
End Property
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
Public Class RssFeed
Private _title As String
Private _link As String
Private _language As String
Private _items As IEnumerable(Of RssItem)
Public Property Title() As String
Get
Return _title
End Get
Set(ByVal value As String)
_title = value
End Set
End Property
Public Property Link() As String
Get
Return _link
End Get
Set(ByVal value As String)
_link = value
End Set
End Property
Public Property Language() As String
Get
Return _language
End Get
Set(ByVal value As String)
_language = value
End Set
End Property
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
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
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