2 people like it.

Generate rss feed from F# record items

Generate rss news feed from a list of F# record items using Linq2Xml. To use e.g. to generate feeds from your web server.

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
13: 
14: 
15: 
16: 
17: 
18: 
19: 
20: 
21: 
22: 
23: 
24: 
25: 
26: 
27: 
28: 
29: 
30: 
31: 
32: 
33: 
34: 
35: 
36: 
37: 
38: 
39: 
40: 
41: 
42: 
43: 
44: 
45: 
46: 
47: 
48: 
49: 
50: 
51: 
52: 
53: 
54: 
55: 
56: 
57: 
58: 
59: 
#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
*)
namespace System
namespace System.Xml
namespace System.Xml.Linq
type NewsItem =
  {Title: string;
   Link: string;
   ReleaseDate: DateTime;
   Description: string;}

Full name: Script.NewsItem
NewsItem.Title: string
Multiple items
val string : value:'T -> string

Full name: Microsoft.FSharp.Core.Operators.string

--------------------
type string = String

Full name: Microsoft.FSharp.Core.string
NewsItem.Link: string
NewsItem.ReleaseDate: DateTime
Multiple items
type DateTime =
  struct
    new : ticks:int64 -> DateTime + 10 overloads
    member Add : value:TimeSpan -> DateTime
    member AddDays : value:float -> DateTime
    member AddHours : value:float -> DateTime
    member AddMilliseconds : value:float -> DateTime
    member AddMinutes : value:float -> DateTime
    member AddMonths : months:int -> DateTime
    member AddSeconds : value:float -> DateTime
    member AddTicks : value:int64 -> DateTime
    member AddYears : value:int -> DateTime
    ...
  end

Full name: System.DateTime

--------------------
DateTime()
   (+0 other overloads)
DateTime(ticks: int64) : unit
   (+0 other overloads)
DateTime(ticks: int64, kind: DateTimeKind) : unit
   (+0 other overloads)
DateTime(year: int, month: int, day: int) : unit
   (+0 other overloads)
DateTime(year: int, month: int, day: int, calendar: Globalization.Calendar) : unit
   (+0 other overloads)
DateTime(year: int, month: int, day: int, hour: int, minute: int, second: int) : unit
   (+0 other overloads)
DateTime(year: int, month: int, day: int, hour: int, minute: int, second: int, kind: DateTimeKind) : unit
   (+0 other overloads)
DateTime(year: int, month: int, day: int, hour: int, minute: int, second: int, calendar: Globalization.Calendar) : unit
   (+0 other overloads)
DateTime(year: int, month: int, day: int, hour: int, minute: int, second: int, millisecond: int) : unit
   (+0 other overloads)
DateTime(year: int, month: int, day: int, hour: int, minute: int, second: int, millisecond: int, kind: DateTimeKind) : unit
   (+0 other overloads)
NewsItem.Description: string
val myChannelFeed : channelTitle:string -> channelLink:string -> channelDescription:string -> items:NewsItem list -> XDocument

Full name: Script.myChannelFeed
val channelTitle : string
val channelLink : string
val channelDescription : string
val items : NewsItem list
type 'T list = List<'T>

Full name: Microsoft.FSharp.Collections.list<_>
val xn : (string -> XName)
type XName =
  member Equals : obj:obj -> bool
  member GetHashCode : unit -> int
  member LocalName : string
  member Namespace : XNamespace
  member NamespaceName : string
  member ToString : unit -> string
  static member Get : expandedName:string -> XName + 1 overload

Full name: System.Xml.Linq.XName
XName.Get(expandedName: string) : XName
XName.Get(localName: string, namespaceName: string) : XName
val elem : (string -> string -> XElement)
val name : string
val valu : string
Multiple items
type XElement =
  inherit XContainer
  new : name:XName -> XElement + 4 overloads
  member AncestorsAndSelf : unit -> IEnumerable<XElement> + 1 overload
  member Attribute : name:XName -> XAttribute
  member Attributes : unit -> IEnumerable<XAttribute> + 1 overload
  member DescendantNodesAndSelf : unit -> IEnumerable<XNode>
  member DescendantsAndSelf : unit -> IEnumerable<XElement> + 1 overload
  member FirstAttribute : XAttribute
  member GetDefaultNamespace : unit -> XNamespace
  member GetNamespaceOfPrefix : prefix:string -> XNamespace
  member GetPrefixOfNamespace : ns:XNamespace -> string
  ...

Full name: System.Xml.Linq.XElement

--------------------
XElement(name: XName) : unit
XElement(other: XElement) : unit
XElement(other: XStreamingElement) : unit
XElement(name: XName, content: obj) : unit
XElement(name: XName, [<ParamArray>] content: obj []) : unit
val elems : XElement list
Multiple items
module List

from Microsoft.FSharp.Collections

--------------------
type List<'T> =
  | ( [] )
  | ( :: ) of Head: 'T * Tail: 'T list
  interface IEnumerable
  interface IEnumerable<'T>
  member GetSlice : startIndex:int option * endIndex:int option -> 'T list
  member Head : 'T
  member IsEmpty : bool
  member Item : index:int -> 'T with get
  member Length : int
  member Tail : 'T list
  static member Cons : head:'T * tail:'T list -> 'T list
  static member Empty : 'T list

Full name: Microsoft.FSharp.Collections.List<_>
val sortBy : projection:('T -> 'Key) -> list:'T list -> 'T list (requires comparison)

Full name: Microsoft.FSharp.Collections.List.sortBy
val i : NewsItem
val map : mapping:('T -> 'U) -> list:'T list -> 'U list

Full name: Microsoft.FSharp.Collections.List.map
namespace System.Net
type WebUtility =
  static member HtmlDecode : value:string -> string + 1 overload
  static member HtmlEncode : value:string -> string + 1 overload

Full name: System.Net.WebUtility
Net.WebUtility.HtmlEncode(value: string) : string
Net.WebUtility.HtmlEncode(value: string, output: IO.TextWriter) : unit
DateTime.ToString() : string
DateTime.ToString(provider: IFormatProvider) : string
DateTime.ToString(format: string) : string
DateTime.ToString(format: string, provider: IFormatProvider) : string
Multiple items
type XDocument =
  inherit XContainer
  new : unit -> XDocument + 3 overloads
  member Declaration : XDeclaration with get, set
  member DocumentType : XDocumentType
  member NodeType : XmlNodeType
  member Root : XElement
  member Save : fileName:string -> unit + 6 overloads
  member WriteTo : writer:XmlWriter -> unit
  static member Load : uri:string -> XDocument + 7 overloads
  static member Parse : text:string -> XDocument + 1 overload

Full name: System.Xml.Linq.XDocument

--------------------
XDocument() : unit
XDocument([<ParamArray>] content: obj []) : unit
XDocument(other: XDocument) : unit
XDocument(declaration: XDeclaration, [<ParamArray>] content: obj []) : unit
Multiple items
type XDeclaration =
  new : other:XDeclaration -> XDeclaration + 1 overload
  member Encoding : string with get, set
  member Standalone : string with get, set
  member ToString : unit -> string
  member Version : string with get, set

Full name: System.Xml.Linq.XDeclaration

--------------------
XDeclaration(other: XDeclaration) : unit
XDeclaration(version: string, encoding: string, standalone: string) : unit
Multiple items
type XAttribute =
  inherit XObject
  new : other:XAttribute -> XAttribute + 1 overload
  member IsNamespaceDeclaration : bool
  member Name : XName
  member NextAttribute : XAttribute
  member NodeType : XmlNodeType
  member PreviousAttribute : XAttribute
  member Remove : unit -> unit
  member SetValue : value:obj -> unit
  member ToString : unit -> string
  member Value : string with get, set
  ...

Full name: System.Xml.Linq.XAttribute

--------------------
XAttribute(other: XAttribute) : unit
XAttribute(name: XName, value: obj) : unit
val box : value:'T -> obj

Full name: Microsoft.FSharp.Core.Operators.box

More information

Link:http://fssnip.net/7QI
Posted:8 years ago
Author:Tuomas Hietanen
Tags: rss , feed