Umbraco 7 - Partial View - Get RSS Feed and Check Status Code - C# - Razor

The following script displays an RSS feed from another web site and first cehcks to see if the feed is available.

@inherits Umbraco.Web.Mvc.UmbracoTemplatePage
@using System.Xml;
@{      
           var rssFeedTitle = @ViewData["RSStitle"];
  var rssFeedURL = @ViewData["RSSURL"];
 }

@{
<h3>@rssFeedTitle</h3>

var request = (HttpWebRequest)WebRequest.Create("https://jobs.gmc-uk.org/rss/rss200_jobs_2965.xml");
request.Method = "HEAD";


try
{
var response = (HttpWebResponse)request.GetResponse();

//Get the XML from remote URL
XmlDocument xml = new XmlDocument();

//URL currently hardcoded - but you could use a macro param to pass in URL
xml.Load("" + @rssFeedURL);


//Select the nodes we want to loop through
//XmlNodeList nodes = xml.SelectNodes("//item[position() <= 3]");
XmlNodeList nodes = xml.SelectNodes("//item");

//Traverse the entire XML nodes.
foreach (XmlNode node in nodes)
{

var title = node.SelectSingleNode("title").InnerText;
var link = node.SelectSingleNode("link").InnerText;

<h2><a href="@link" target="_blank" title="@title">@title</a></h2>

try
{
var description = node.SelectSingleNode("description").InnerText;

if(description.Length > 300)
  {
@Html.Raw(description.Substring(0,300))
  }
  else
  {
  <p>@Html.Raw(@description)</p>
  }
<p><a href="@link" target="_blank" title="@title">View full details</a></p>
}
catch{}

}
}
catch{}
}

------------

This following code should be in your teplate calling the Partial View:

@Html.Partial("RSSFeedGeneric", new ViewDataDictionary{{"RSStitle","GMC Jobs"},{"RSSURL","https://jobs.gmc-uk.org/rss/rss200_jobs_2965.xml"}} )

Comments

Popular posts from this blog

Umbraco Razor Sort Nodes Ascending or Descending

Umbraco Razor get Querystring

Create a .NET Contact Form that Gets the Last Url Visited in C# Can also be Used in Umbraco