To build a dictionary from seq, you usually use the dict function. It occupies several key / values ββof tuples, so the problem boils down to creating this sequence of tuples. With System.Xml.Linq, you move the XML to get the attribute values ββin the sequence.
Here is the code:
let x = @"<Overrides> <token key='A' value='1'/> <token key='B' value='2'/> <token key='C' value='3'/> <token key='D' value='4'/> <token key='E' value='5'/> <token key='F' value='6'/> </Overrides>" #if INTERACTIVE #r "System.Xml" #r "System.Xml.Linq" #endif open System.Xml.Linq let docs = XDocument.Parse x let elem = docs.Root.Descendants(XName.Get "token") |> Seq.map (fun x -> x.Attribute(XName.Get "key").Value, x.Attribute(XName.Get "value").Value) |> dict
Seq.head returns the first element of the sequence; it is not useful here if you do not want to use it to go to the Overrides node.
Seq.iter applies the function to each element in a sequence similar to a for / foreach , only for the side-effects of the function. It is not useful here if you do not want to build a dictionary imperatively, i.e. Add key / values ββto an existing dictionary.
source share