Monday, July 23, 2012

Read Rss feed using vb.net

the following code sample read from Rss Xml File and write file content as html inside iframe in web page .
Imports System.Xml
Imports System.ServiceModel.Syndication

 Sub Press_News_Get()
 
        Try
            Dim ARB As String = Server.MapPath("intranet/Saudi_Press_Agent.xml")
            Dim reader As New XmlTextReader(ARB)
            Dim feed As SyndicationFeed = SyndicationFeed.Load(reader)

            For Each item As SyndicationItem In feed.Items
                Dim NewTitle As String = item.Title.Text
                Dim url As String = item.Links(0).Uri.ToString()
                Dim lnk As New HtmlAnchor
                lnk.InnerHtml = NewTitle
                lnk.HRef = url
                lnk.Target = "_blank"
                ml.Controls.Add(New LiteralControl("<span class=""sep"">وكالة الأنباء السعودية</span>"))
                ml.Controls.Add(lnk)
                ml.Attributes.Add("style", "color:blue;;margin:0px;padding:0px")

                ml.Controls.Add(New LiteralControl("&nbsp;&nbsp;&nbsp;&nbsp;"))
                lnk.Attributes.Add("style", "text-decoration:none;margin:0px;padding:0px")
            Next
 Catch ex As Exception
            Response.Write(ex.Message)
        End Try

    End Sub

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