Thursday, April 22, 2010

Removing html tags in coding, .Net Syndication

protected void Page_Load(object sender, EventArgs e)
{
// Get the books ordered by pubdate in descending order
//PubsDataContext db = new PubsDataContext();
//var dataItems = from book in db.Titles
// orderby book.pubdate descending
// select book;
// Determine the maximum number of items to show in the feed
DataSet ds = new RSSBAL().GetRSS();
const int maxItemsInFeed = 10;
// Determine whether we're outputting an Atom or RSS feed
bool outputRss = (Request.QueryString["Type"] == "RSS");
bool outputAtom = !outputRss;
// Output the appropriate ContentType
if (outputRss)
Response.ContentType = "application/rss+xml";
else if (outputAtom)
Response.ContentType = "application/atom+xml";
// Create the feed and specify the feed's attributes
SyndicationFeed myFeed = new SyndicationFeed();
myFeed.Title = TextSyndicationContent.CreatePlaintextContent("Published Pages by Ingram");
myFeed.Description = TextSyndicationContent.CreatePlaintextContent("A syndication of the most recently published Pages by Ingram.");
myFeed.Links.Add(SyndicationLink.CreateAlternateLink(new Uri(GetFullyQualifiedUrl("~/Home.aspx"))));
myFeed.Links.Add(SyndicationLink.CreateSelfLink(new Uri(GetFullyQualifiedUrl(Request.RawUrl))));
myFeed.Copyright = TextSyndicationContent.CreatePlaintextContent("Copyright Ingram Micro");
myFeed.Language = "en-us";
// Creat and populate a list of SyndicationItems
List feedItems = new List();
foreach (DataRow t in ds.Tables[0].Rows)
{
// Atom items MUST have an author, so if there are no authors for this content item then go to next item in loop
//if (outputAtom && t.TitleAuthors.Count == 0)
// continue;
SyndicationItem item = new SyndicationItem();
item.Title = TextSyndicationContent.CreatePlaintextContent(t["PageTitle"].ToString());
item.Links.Add(SyndicationLink.CreateAlternateLink(new Uri(GetFullyQualifiedUrl(t["URL"].ToString()))));
item.Summary = TextSyndicationContent.CreatePlaintextContent(StripTagsCharArray(t["PageDesc"].ToString()));
item.PublishDate = Convert.ToDateTime(t["ModifiedDate"]);
if (Convert.ToBoolean(t["IsMain"]) == true)
{
item.Categories.Add(new SyndicationCategory("Main Pages"));
}
else
{
item.Categories.Add(new SyndicationCategory("Sub Pages"));
}
// Add the item to the feed
feedItems.Add(item);
}
myFeed.Items = feedItems;
// Return the feed's XML content as the response
XmlWriterSettings outputSettings = new XmlWriterSettings();
outputSettings.Indent = true; //(Uncomment for readability during testing)
XmlWriter feedWriter = XmlWriter.Create(Response.OutputStream, outputSettings);
if (outputAtom)
{
// Use Atom 1.0
Atom10FeedFormatter atomFormatter = new Atom10FeedFormatter(myFeed);
atomFormatter.WriteTo(feedWriter);
}
else if (outputRss)
{
// Emit RSS 2.0
Rss20FeedFormatter rssFormatter = new Rss20FeedFormatter(myFeed);
rssFormatter.WriteTo(feedWriter);
}
feedWriter.Close();
}
private string GetFullyQualifiedUrl(string url)
{
return string.Concat(Request.Url.GetLeftPart(UriPartial.Authority), ResolveUrl(url));
}
public static string StripTagsCharArray(string source)
{
char[] array = new char[source.Length];
int arrayIndex = 0;
bool inside = false;
for (int i = 0; i < source.Length; i++)
{
char let = source[i];
if (let == '<')
{
inside = true;
continue;
}
if (let == '>')
{
inside = false;
continue;
}
if (!inside)
{
array[arrayIndex] = let;
arrayIndex++;
}
}
return new string(array, 0, arrayIndex);
}

No comments:

Post a Comment