![]() The code for the method is available in this previous article (first block of code). UrlTidy.ToCleanUrl takes the existing article title, and replaces spaces with dashes etc to give a clean SEO-friendly url. The link node within the item elements has an odd method applied to the item.Head to create a link. Not only that, but you can almost see the outline of the finished XML document from the code. ![]() The savings mainly derive from not having to explicitly close elements in the document, and not having to write out the individual parts of an element using different methods such as WriteElementString(), WriteStartElement() etc. If you compare LINQ to XML with using the XmlTextWriter, you can see that a lot less code is required. Return Content(rss.ToString(), "text/xml") New XElement( "link", String.Format(url, item.ID, UrlTidy.ToCleanUrl(item.Head))), New XElement( "description", item.Intro), New XElement( "copyright", "(c)" + + ", Mikesdotnetting. New XElement( "description", "Latest additions to Mikesdotnetting"), New XElement( "title", "Mikesdotnetting News Feed"), Var rss = new XDocument( new XDeclaration( "1.0", "utf-8", "yes"), We will look at the code that does that, then examine it: public ContentResult RSS() So now I need to add a method to generate the RSS feed, which is actually just a streamed XML document. If I decide to get fed up with Entity Framework, and choose to use LINQ to SQL, or even ADO.NET code for data access, I will only have to change this class in one place, rather than have to go through every method and unhook the concrete ArticleRepository class from them. This controller is making use of the Repository pattern, in that instead of invoking the ArticleRepository and calling it directly, I am programming against an interface instead. Public XMLController( IArticleRepository rep) Public XMLController() : this( new ArticleRepository()) I also need to create a Controller to handle this (and the sitemap), so I shall call it XMLController: using System Starting with the RSS feed, I have created a method that lists the most recent 20 items that have been added: public IEnumerable GetRSSFeed() The actual data retrieval is in a class called ArticleRepository, which is in the Model area of the application. My full blown Article class consists of the text of the article and a lot of meta-data that just isn't necessary for RSS or a sitemap, and I have already created a couple of classes to cater for the retrieval of a small subset of Article data, so I shall use those. The content will be drawn from a database and the Entity Framework is the mechanism I have chosen to act as my data access technology. This is so that the documents incorporate the most up-to-date content as articles are added and amended regularly. Both of the documents will be generated on demand - that is as a result of someone requesting the appropriate URL. LINQ to XML requires far less code to achieve the same thing. I've already looked at both RSS Feeds and Google Sitemaps before, using an XMLTextWriter object to generate the output, and it can be a fairly laborious task.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |