26 people like it.

Functional XML Construction and Query

This snippet shows how to construct and query XML using XLinq without too many helper functions.

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
13: 
open System.Xml.Linq

let xname str = XName.Get str

let sitemapEntry = 
    XElement(xname "url",
        XElement(xname "loc", "http://fssnip.net/"),
        XElement(xname "lastmod", "2011-03-11"),
        XElement(xname "changefreq", "daily"),
        XElement(xname "priority", 0.5)
    )

let priority = sitemapEntry.Descendants() |> Seq.tryFind (fun x -> x.Name.LocalName = "priority")
namespace System
namespace System.Xml
namespace Microsoft.FSharp.Linq
val xname : str:'a -> 'b

Full name: Script.xname
val str : 'a
val sitemapEntry : obj

Full name: Script.sitemapEntry
val priority : obj option

Full name: Script.priority
module Seq

from Microsoft.FSharp.Collections
val tryFind : predicate:('T -> bool) -> source:seq<'T> -> 'T option

Full name: Microsoft.FSharp.Collections.Seq.tryFind
val x : obj
Raw view New version

More information

Link:http://fssnip.net/3r
Posted:6 years ago
Author:Taha Hachana
Tags: xml , xlinq