It's a work in progress and part of a bigger project (I'll probably open-source it eventually).
My code reads a list of RSS feeds from another XML file that loads in the FeedListParams containing the MessageID to update and some stuff to show pics, dates, etc. (I used a structure instead of a class for quick & dirty coding)
Public Structure FeedListParams
Public feeduri As String
Public feedtitle As String
Public messageid As Integer ' the MessageID of the SQL table
Public upperlimit As Integer 'how many RSS feed records to show
Public truncatetochars As Integer 'truncate the Description to this many chars
Public showdate As Boolean
Public showimages As Boolean
Public saveimageslocally As Boolean 'not implemented, but will grab any remote pics, resize& save
Public resizeimages As Boolean
Public maxpixels As Integer 'maximum width pixels - for formatting
Public showasseparatetopics As Boolean 'not implemented yet, but I plan on automatically putting in each feed record into their own separate topics
End Structure
'the FeedItem structure (you can use a class, but, once again, quick & dirty code)
Public Structure FeedItem
Public title As String
Public link As String
Public pubDate As String
Public description As String
Public enclosure As XmlNode 'this node usually has a picture URI - at least WordPress RSS feeds do
End Structure
'in another function, you can call Message = ProcessRSS(f) where f is the structure FeedListParams
'the Message string variable will contain the entire message.
You first have to create the initial topic (and put a filler character to start), make the topic "sticky", find the corresponding MessageID (for that Topic) from the table in the database so you know where to update it.
Public Function ProcessRSS(f As FeedListParams) As String
Dim request As WebRequest = WebRequest.Create(f.feeduri)
Dim response As WebResponse = request.GetResponse()
Dim sb As New StringBuilder("")
Dim rssStream As Stream = response.GetResponseStream()
Dim rssDoc As New XmlDocument()
rssDoc.Load(rssStream)
Dim rssItems As XmlNodeList = rssDoc.SelectNodes("rss/channel/item")
Dim Item As FeedItem
Dim totalCount As Integer = rssItems.Count
If totalCount > f.upperlimit Then
totalCount = f.upperlimit
End If
If totalCount > 0 Then
sb.Append("
" + f.feedtitle + "
")
sb.Append("
")
sb.Append("
") Dim i As Integer = 0
While i < totalCount
sb.Append("
- ")
Dim imageURI As String = "../images/sp75.png" 'padding pic
If f.showimages Then
Item.enclosure = rssItems.Item(i).SelectSingleNode("enclosure")
If Item.enclosure IsNot Nothing Then
imageURI = ObjToStr(Item.enclosure.Attributes("url").Value)
End If
End If
sb.Append("")
Item.title = ObjToStr(rssItems.Item(i).SelectSingleNode("title").InnerXml.ToString.Trim).Replace("'", "''")
Item.link = ObjToStr(rssItems.Item(i).SelectSingleNode("link").InnerXml.ToString.Trim).Replace("'", "''")
sb.Append("" + Item.title + "
")
Item.pubDate = String.Empty
If f.showdate Then
Item.pubDate = ObjToDate(rssItems.Item(i).SelectSingleNode("pubDate").InnerXml.ToString.Trim).ToShortDateString
sb.Append("
Published On: " & Item.pubDate & "
") End If
'NOTE: I replaced the CDATA tags via simple replace - there are better ways of doing this
Item.description = ObjToStr(rssItems.Item(i).SelectSingleNode("description").InnerXml.ToString.Trim).Replace("", "").Replace("’", "'").Replace("—", "--").Replace("'", "''")
sb.Append("
" & Item.description & "
") sb.Append("
") i += 1
End While
sb.Append("
")
End If
Return sb.ToString()
End Function
SOME CSS: (added into the theme css file)
#rsstitle {
color: #295885;
font-size: 12px;
}
.rsswrap {
font-family: Verdana;
line-height: 15px;
color: #000;
}
.rsswrap ul li {
background: none repeat scroll 0 0 rgba(0, 0, 0, 0);
border-bottom: 1px solid #E8E8E8;
display: block;
float: left;
font-size: 11px;
list-style-type: none;
padding: 10px 0;
width: 100%;
}
.rsswrap ul li img {
float: left;
margin-right: 5px;
margin-bottom: 30px;
width: 75px;
border: 0 none;
outline: medium none;
}
.rsswrap ul li a {
color: #295885;
text-decoration: none;
}
.rsswrap ul li a h3 {
color: #295885;
text-decoration: none;
font-size: 14px;
margin: 0;
padding: 0;
}
.rsspubdate {
color: #959595;
font-size: 10px;
}
.rsswrap ul li p {
padding-bottom: 14px;
font-size: 12px;
margin-top: 5px;
margin-bottom: 5px;
}
The stored proc:
CREATE PROCEDURE [dbo].[RSS_EXT_UpdateMessage]
(
@MessageID int,
@Message ntext
)
AS
BEGIN
SET NOCOUNT ON;
BEGIN TRY
update yaf_Message
set [Message]=@Message
where messageID=@MessageID;
SELECT 'SUCCESS';
END TRY
BEGIN CATCH
SELECT 'ERROR';
END CATCH;
END