1.RssFeed Solid Class
1 /// <summary>2 ///RssFeed entity class3 /// </summary>4 Public classRssFeed5 {6 /// <summary>7 ///title8 /// </summary>9 Public stringTitle {Get;Set; }Ten One /// <summary> A ///links - /// </summary> - Public stringLink {Get;Set; } the - /// <summary> - ///Description - /// </summary> + Public stringDescription {Get;Set; } - + /// <summary> A ///Publication Time at /// </summary> - PublicDateTime Publishdate {Get;Set; } -}View Code
2.RssFeed Helper Classes
1 /// <summary>2 ///Rssfeedhelper3 /// </summary>4 Public Static classRssfeedhelper5 {6 /// <summary>7 ///Generate RssFeed8 /// </summary>9 /// <param name= "Rssfeedlist" >rssfeed List</param>Ten /// <returns>RssFeed</returns> One Public StaticXDocument Getrssfeed (list<rssfeed>rssfeedlist) A { -XElement channel =NewXElement ("Channel");//Channel Node -Channel. ADD (Newxelement[]{ the NewXElement ("title","Test"),//Channel Title - NewXElement ("Link","http://localhost"),//Page Links - NewXElement ("Description","Test RSS")//Channel Description - }); + - foreach(varRssFeedinchRssfeedlist)//to process each element in the RssFeed collection + { AXElement item =NewXElement ("Item",Newxelement[]{//to generate a new item node at NewXElement ("title", Rssfeed.title),//to add a child node to the new item node - NewXElement ("Description", rssfeed.description), - NewXElement ("Link", Rssfeed.link), - NewXElement ("pubDate", Rssfeed.publishdate) - }); -Channel. ADD (item);//Add a new item node to the channel in } - toXDocument doc =NewXDocument ( + NewXdeclaration ("1.0","Utf-8","Yes"),//XML Document Declaration - NewXElement ("RSS",//root node the NewXAttribute ("version","2.0"),//Properties of RSS nodes * NewXElement (channel//RSS Sub-node channel $ )));Panax Notoginseng - returnDoc; the } +}View Code
3. Create the Rssfeedresult class, inherited from ActionResult, to return RSS in the controller of ASP.
1 Public classRssfeedresult:actionresult2 {3List<rssfeed> Data {Get;Set; }4 5 PublicRssfeedresult (list<rssfeed>data)6 {7Data =data;8 }9 Ten Public Override voidExecuteresult (controllercontext context) One { A if(Context = =NULL) - { - Throw NewArgumentNullException ("Context"); the } - -Httpresponsebase response =context. Httpcontext.response; -Response. ContentType ="Text/xml";//set the contenttype in the HTTP header +XDocument result= rssfeedhelper.getrssfeed (Data);//Get XML Data -Response. Write (result. ToString ());//writing XML data to response + } A}View Code
4. Call the Rssfeedresult (RSS) method in the controller to return to the RSS page
1 <RSSversion= "2.0">2 <Channel>3 <title>Channel Title</title>4 <Link>Web address</Link>5 <Description>Channel description</Description>6 <Item>7 <title>Content 1 title</title>8 <Description>Content 1 Description</Description>9 <Link>Content 1 Links</Link>Ten </Item> One <Item> A <title>Content 2 title</title> - <Description>Content 2 Description</Description> - <Link>Content 2 Links</Link> </Item> the </Channel> - </RSS>
View Code
MVC Generation rssfeed[Collation]