1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
| using System;
using System.ComponentModel;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using System.Text.RegularExpressions;
using System.Xml;
using System.Xml.XPath;
namespace WebPartRSS.MyWebPartRSS
{
[ToolboxItemAttribute(false)]
public class MyWebPartRSS : WebPart
{
protected override void CreateChildControls()
{
string rssfeed = "http://feeds2.feedburner.com/dfblogs";
DisplayRSS(rssfeed);
}
protected string DisplayRSS(string ListRssUrl)
{
try
{
string rss_text = "";
string title = "";
string link = "";
string description = "";
XPathDocument doc = new XPathDocument(ListRssUrl);
XPathNavigator nav = doc.CreateNavigator();
XPathNodeIterator items = nav.Select("//item");
int items_to_show = 2;
int count = 0;
while (items.MoveNext() && count < items_to_show)
{
XPathNavigator navCurrent = items.Current;
try
{
title = navCurrent.SelectSingleNode("title").InnerXml;
link = navCurrent.SelectSingleNode("link").InnerXml;
description = navCurrent.SelectSingleNode("description").InnerXml;
}
catch { }
CleanUp(ref title, ref description); // Calling a FeedClean Function
if (description != "")
{
description = "- " + description;
description += "...";
}
rss_text += "<a href='" + link + "' target='_blank'>" + title + "</a> " + description;
if (count < items_to_show - 1)
rss_text += "<br/><br/>";
rss_text += "</div>";
count++;
}
return rss_text;
}
catch
{ }
}
protected void CleanUp(ref string title, ref string description)
{
title = title.Replace("\n", " ");
title = title.Replace("\r", " ");
description = description.Replace("<", "<");
description = description.Replace(">", ">");
description = Regex.Replace("hover_description", @"<(.\n)*?>", string.Empty); // To Remove all the html tags from the RSS feed.
title = title.Trim();
description = description.Trim();
}
}
} |
Partager