How to read RSS news on Blazor WebAssembly ?


Recently I got the chance to implement google RSS news on one of my Blazor Webassembly project. It is very simple to implement using System.ServiceModel.Syndication package

Step 1: Create the Class like

 public class NewsItem
    {
        public string NewsTitle { get; set; } = "";
        public string NewsLink { get; set; } = "";
        public DateTime PubishDate { get; set; }
    }

 

Step2: Create the Web API controller and write the method for reading RSS like this

 [HttpGet("GetNewsFeedData")]
        public List<NewsItem> GetNewsFeedData() 
        {
            List<NewsItem> newsItems = new List<NewsItem>();
            List<NewsItem> healthData = new List<NewsItem>();

            string url = "https://news.google.com/rss/search?q=prescription+drugs&hl=en-US&gl=US&ceid=US:en";
            newsItems = FetchRssFeedData(url);

            string healthrssPath = "https://news.google.com/rss/search?q='health'";
            healthData= FetchRssFeedData(healthrssPath);
            var combineList = newsItems.Concat(healthData).ToList();
            var result= (from m in combineList orderby m.PubishDate descending
                        select m).ToList();
            return result;
        }

 public List<NewsItem> FetchRssFeedData(string path)
        {
            List<NewsItem> NewsDrugs = new List<NewsItem>();
            XmlReader reader = XmlReader.Create(path);
            SyndicationFeed feed = SyndicationFeed.Load(reader);
            reader.Close();
            foreach (SyndicationItem item in feed.Items)
            {
                NewsItem newsItem = new();
                newsItem.NewsTitle = item.Title.Text;
                newsItem.NewsLink = item.Links[0].Uri.ToString();
                newsItem.PubishDate = (DateTime)item.PublishDate.UtcDateTime;
                NewsDrugs.Add(newsItem);
            }
            return NewsDrugs;
        }

Step 3: Consume on this api on Razor page like

@if (NewsDrugs != null)
{
    @foreach (var item in NewsDrugs)
    {
        <div class="container">
            <div class="row">
              <a href="@item.NewsLink" target="_blank" rel="noopener noreferrer">@item.NewsTitle</a>
            </div>
            <div class="row">
                <b>PubishDate : </b> @item.PubishDate.ToString("MM/dd/yyyy")
            </div>
            <hr />
        </div>
    }
}

@code {
    public List<NewsItem> NewsDrugs { get; set; }

    protected override async Task OnInitializedAsync()
    {
        NewsDrugs = await DiseaseDrugRepository.GetNewsItems();
        await base.OnInitializedAsync();
        StateHasChanged();
    }
}

Note: For Blazor Server, no need to create web api layer, you can directly write on your razor page.

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.