Showing posts with label asp.net Rss. Show all posts
Showing posts with label asp.net Rss. Show all posts

Sunday, July 22, 2012

Create Rss Using System.ServiceModel.Syndication

To Create Rss Using System.ServiceModel.Syndication
add reference for the library in your project .

step2: Create your Rss XML using this function sample
Public Function GetNewsRss() As Rss20FeedFormatter
        Dim feed As SyndicationFeed = New SyndicationFeed()
        feed.Authors.Add(New SyndicationPerson("info@mwa.gov.sa"))
        'feed.Categories.Add(New SyndicationCategory("How To Sample Code"))
        feed.Description = New TextSyndicationContent("أخر الاخبار الخاصة بالمديرية العامة للمياه بمنطقة المدينة المنورة")
        feed.Title = New TextSyndicationContent("أخبار المديرية العامة للمياه بمنطقة المدينة المنورة")
        feed.Copyright = New TextSyndicationContent("جميع الحقوق محفوظة لموقع المديرية العامة للمياه بمنطقة المدينة المنورة")
        feed.Links.Add(New SyndicationLink(New Uri("http://www.mwa.gov.sa/index.aspx")))

        Dim objConnection As New SqlConnection(ConfigurationManager.ConnectionStrings("mwa_mwa_gov_saConnectionString").ConnectionString)
        Dim sql As String = "SELECT News.FrontID,News.Headline, News.InnerNews, NewsImage.NImage, News.NImage_ID, News.LinkType, News.MoreLink, News.Headline FROM News LEFT OUTER JOIN NewsImage ON News.NImage_ID = NewsImage.NImage_ID WHERE (News.ShowFront = 'true') ORDER BY News.FColOrder"
        Dim adp As New SqlDataAdapter(sql, objConnection)
        Dim ds As New DataSet
        objConnection.Open()
        adp.Fill(ds)
        objConnection.Close()
        Dim items As New List(Of SyndicationItem)()
        If (ds.Tables(0).Rows.Count > 0) Then
            Dim i As Integer
            For i = 0 To ds.Tables(0).Rows.Count - 1
                Dim NewItem As SyndicationItem = New SyndicationItem( _
                           ds.Tables(0).Rows(i)("Headline").ToString(), _
                           ds.Tables(0).Rows(i)("InnerNews").ToString(), _
                           New Uri("http://www.mwa.gov.sa/News.aspx?ID=" & ds.Tables(0).Rows(i)("FrontID").ToString()), _
                           "New_" & i, _
                            DateTime.Now)
                items.Add(NewItem)

            Next
            feed.Items = items
        End If
        Return New Rss20FeedFormatter(feed)
    End Function

------------------------------------------------------------------------
step3: Response in page load with Rss20FeedFormatter returned from your previous function .

 Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Response.Clear()
        Response.ContentEncoding = System.Text.Encoding.UTF8
        Response.ContentType = "text/xml"

        Dim rssWriter As XmlWriter = XmlWriter.Create(Response.Output)
        Dim rssFormatter As New Rss20FeedFormatter()
        rssFormatter = GetNewsRss()
        rssFormatter.WriteTo(rssWriter)
        rssWriter.Close()

        Response.End()

    End Sub