I have the following code to deal several different xml files (different scheme) which all have node of <parameters>
. Now I found the match
part will be used in many place so I want to create a function for it.
let xml1 = XmlProvider<"./file1.xml">.Parse(resp)
match xml1.Parameters |> Seq.tryFind (fun x -> x.Name = "token") with
| Some value -> value.Value |> Some
| None -> None
let xml2 = XmlProvider<"./file2.xml">.Parse(resp) // different scheme but has <parameters>
match xml2.Parameters |> Seq.tryFind (fun x -> x.Name = "token") with
| Some value -> value.Value |> Some
| None -> None
// more very different files but has <parameters> ......
In the above example you can see the match
part is repeated. How to define the function?
let getToken (parameters : <what the type?>) =
match parameters |> Seq.tryFind (fun x -> x.Name = "token") with
| Some value -> value.Value |> Some
| None -> None
What the type of parameters
should be?
Update: I've updated the question. BTW, is this a case to show the usefulness of Structural typing?
Here is the code (not workable yet) for testing. xml1
and xml2
have totally different xml scheme except they both have the <parameters>
part.
let input1 = """<r1><parameters><parameter name="token">1</parameter><parameter name="other">xxx</parameter></parameters><othersOf1>..sample....</othersOf1></r1>"""
let xml1 = XmlProvider<"""<r1><parameters><parameter name="token">1</parameter><parameter name="other">xxx</parameter></parameters><othersOf1>...</othersOf1></r1>""">.Parse(input1)
let input2 = """<r2><parameters><parameter name="token">1</parameter><parameter name="other">xxx</parameter></parameters><othersOf2>...sample...</othersOf2></r2>"""
let xml2 = XmlProvider<"""<r2><parameters><parameter name="token">1</parameter><parameter name="other">xxx</parameter></parameters><othersOf2>...</othersOf2></r2>""">.Parse(input2)
let getToken (parameters: ????) =
match parameters |> Seq.tryFind (fun x -> x.Name = "token") with
| Some value -> value.Value |> Some
| None -> None
let token1 = getToken xml1.Parameters
let token2 = getToken xml2.Parameters