Views: 930
This is how I get a WordPress RSS Feed to use in my Xamarin Forms apps.
- I call the GetRSSFeed() when ever I want to load the RSS Feed from within my Xamarin Forms app.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using System.Xml.Linq;
namespace XamarinWordpressRss
{
public class RSS
{
public string item { get; set; }
public string title { get; set; }
public string link { get; set; }
public string description { get; set; }
public string content_encoded { get; set; }
public DateTime pubDate { get; set; }
public int id { get; set; }
public async Task<List> GetRSSFeed(){
List rss = new List();
using (var client = new HttpClient)
{
string baseAddress = "htts://www.somerssfeedwebsite.com/feed";
client.BaseAddress = new Uri(baseAddress);
client.DefaultRequestHeaders.Accept.Clear();
var uri = new Uri(string.Format(baseAddress", string.Empty));
XmlDocument xmlDoc = new XmlDocument();
var resp = await client.GetAsync(uri);
if (resp.IsSuccessStatusCode)
{
var xml = await resp.Content.ReadAsStringAsync();
XNamespace content = "URI";
var items = XDocument.Parse(xml)
.Descendants("item")
.Select(i => new
{
Title = (string)i.Element("title"),
Description = (string)i.Element("description"),
Link = (string)i.Element("link"),
Encoded = (string)i.Element(content + "encoded"),
PubDate = (string)i.Element("pubDate") }).ToList().OrderBy(r => r.Title);
//
int id = 1;
foreach (var item in items)
{
RSS c = new RSS();
c.title = item.Title;
c.description = item.Description;
c.content_encoded = item.Encoded;
c.link = item.Link;
c.pubDate = Convert.ToDateTime(item.PubDate);
c.id = id;
rss.Add(c);
}
}
else
{
}
}
return rss;
}
}
}