Bu yazımda RSS class'ını sizlerle paylaşıyorum.

Bu yazımda bloğumda kullandığım ve bundan sonra ihtiyaç olan tüm projelerde kullanacağım RSS class'ını sizlerle paylaşıyorum. Birçok kişi için oldukça zahmetli iştir ve birçok kişi buna ihtiyaç duyar. Ben de bloğumu yapmaya başladığımda ihtiyaç duyduğum konuların başında bu geliyordu. Şimdi ise bu işlemi yapacak olan kendi class'ımı hazırladım ve kullanıyorum. Xml bir sonuç üretiyoruz ve bunu Linq to Xml ile basitçe gerçekleştiriyoruz. Ayrıca her türlü .Net projelerimizde (.Net 3.5 gerekli) kullanabileceğimiz bir class oldu. Umarım ihtiyaç duyanlara ve ilgilenenlere faydalı olur.

Hem Asp.net ile kullanımını hem de MVC ile kullanımını göstereceğim. Yazmış olduğum class dosyasını indirip kullanacağınız projeye eklemelisiniz. Ayrıca ben namespace olarak kendi adımı verdim. Bunu değiştirebilirsiniz. Class'ı kullanacağınız yerde verdiğiniz namespace'i en tepeye eklemelisiniz. (using MehmetDuran; gibi.)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Xml.Linq;
using System.IO;
using System.Xml;

namespace MehmetDuran
{
	public class RssItem
	{
		public string Baslik { set; get; }
		public string Tanim { set; get; }
		public DateTime Tarih { set; get; }
		public string Link { set; get; }
	}

	public class Rss
	{
		public string Baslik { set; get; }
		public string Tanim { set; get; }
		public string Link { set; get; }
		public string Author { set; get; }
		public List<RssItem> Items { set; get; }
		public DateTime GuncellenenTarih { set; get; }
		public string Resim { get; set; }

		public Rss(string RssBaslik, string RssTanim, string RssLink, string RssAuthor, List<RssItem> RssItems, DateTime RssGuncellenenTarih, string RssResim)
		{
			this.Baslik = RssBaslik;
			this.Tanim = RssTanim;
			this.Link = RssLink;
			this.Author = RssAuthor;
			this.Items = RssItems;
			this.GuncellenenTarih = RssGuncellenenTarih;
			this.Resim = RssResim;
		}

		public string RssSonuc()
		{
			XDocument doc = new XDocument
			(
				new XDeclaration("1.0", "UTF-8", ""),
				new XElement
				(
					"rss",
					new XAttribute("version", "2.0"),
					new XElement
					(
						"channel",
						new XElement("title", Baslik),
						new XElement("link", Link),
						new XElement("description", Tanim),
						new XElement("image", new XElement("url", Resim))
					)
				)
			);
			foreach (RssItem item in Items)
			{
				XElement i = new XElement
				(
					"item",
					new XElement("title", item.Baslik),
					new XElement("link", item.Link),
					new XElement("pubDate", item.Tarih),
					new XElement("description", item.Tanim)
				);
				doc.Element("rss").Element("channel").Add(i);
			}
			MemoryStream ms = new MemoryStream();
			XmlWriter xw = XmlWriter.Create(ms);
			doc.Save(xw);
			xw.Close();
			return new System.Text.UTF8Encoding().GetString(ms.ToArray());
		}

	}
}


Asp.net ile kullanmamız için kodlara bakalım.

protected void Page_Load(object sender, EventArgs e)  
{    
	List<RssItem> veriler = new List<RssItem>()    
	{      
	// Burada veritabanından alınan veriler ile işlemler yapılacak.      
		new RssItem()      
		{        
			Baslik = "Rss başlığı",        
			Tanim = "Rss içeriği",        
			Link = "http://mehmetduran.com",        
			Tarih = DateTime.Now      
		}    
	};    
	Rss rss = new Rss    
	(      
		"mehmetduran.com - Rss Başlık", // Rss başlığı      
		"mehmetduran.com - Rss Tanım", // Rss tanımı      
		"http://mehmetduran.com/Blog.aspx/RSS", // Rss adresi      
		"Mehmet Duran", // Rss Author      
		veriler, // Rss verilerimiz      
		DateTime.Now, // Rss son güncelleme tarihi      
		"http://mehmetduran.com/Content/style1/mehmet.gif" 
		// Rss resim adresi    
	);    
	Response.Clear();    
	Response.ContentType = "application/xml";    
	Response.Write(rss.RssSonuc());  
}

Asp.net ile bu şekilde bir kullanım ile Rss sayfasını oluşturabiliyoruz. Şimdi de MVC ile kullanıma bakalım.
 

public ActionResult Rss()  
{    
	List<RssItem> veriler = new List<RssItem>()    
	{      
		new RssItem()      
		{        
			Baslik = "Rss başlığı",        
			Tanim = "<h1>Rss içeriği",        
			Tarih = DateTime.Now,        
			Link = "http://mehmetduran.com"      
		}    
	};    
	Rss rss = new Rss    
	(      
		"Mehmet Duran - mehmetduran.com - Rss",       
		"mehmetduran.com - Son Yazılar",       
		"http://mehmetduran.com",       
		"Mehmet Duran",       
		veriler,       
		DateTime.Now,       
		"http://mehmetduran.com/Content/mehmet.gif"    
	);    
	return Content(rss.RssSonuc(), "application/xml");  
}

MVC ile kullanım da bu şekilde. Yazmış olduğum class'ı her türlü projemde kullanmayı düşünüyorum. Gelişecek olan yeniliklere bağlı olarak güncellemeler yapmak oldukça basit olacaktır. Birçok kişinin işlerini de kolaylaştıracağını düşünüyorum.

 

İlgili Makaleler

Bu yazıya 0 yorum yapılmış.

Yorum Gönder