Skip to content

Instantly share code, notes, and snippets.

@Thorium
Last active September 10, 2016 13:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Thorium/00a3bfae0bf97e161f474e2afe0c335c to your computer and use it in GitHub Desktop.
Save Thorium/00a3bfae0bf97e161f474e2afe0c335c to your computer and use it in GitHub Desktop.
Generate RSS feed from a list of F# record items using Linq2Xml. To use e.g. to generate feeds from your web server.
#if INTERACTIVE
#r "System.Xml.Linq.dll"
#else
module Rss
#endif
open System
open System.Xml.Linq
type NewsItem = {
Title : string;
Link: string;
ReleaseDate: DateTime;
Description: string;
}
let myChannelFeed
(channelTitle:string)
(channelLink:string)
(channelDescription:string) (items : NewsItem list) =
let xn = XName.Get
let elem name (valu:string) = XElement(xn name, valu)
let elems =
items |> List.sortBy(fun i -> i.ReleaseDate)
|> List.map(fun i ->
XElement(xn "item",
elem "title" (System.Net.WebUtility.HtmlEncode i.Title),
elem "link" i.Link,
elem "guid" i.Link,
elem "pubDate" (i.ReleaseDate.ToString("r")),
elem "description" (System.Net.WebUtility.HtmlEncode i.Description)
))
XDocument(
XDeclaration("1.0", "utf-8", "yes"),
XElement(xn "rss",
XAttribute(xn "version", "2.0"),
elem "title" channelTitle,
elem "link" channelLink,
elem "description" channelDescription,
elem "language" "en-us",
XElement(xn "channel", elems)
) |> box)
// set mime-type to "text/xml" and deliver e.g. as rss.xml
(*
let generateFeed =
myChannelFeed "FSnip Test Feed" "http://www.fssnip.com" "Some independent F# functions and code"
let myXml =
[{Title = "Welcome";
Link="www.fssnip.net/9q";
ReleaseDate=DateTime.UtcNow;
Description="First item"};
{Title = "Hello";
Link="www.fssnip.net/1q";
ReleaseDate=DateTime.UtcNow;
Description="Second item"}
] |> generateFeed
*)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment