简体   繁体   中英

Deserialization of Xml with F#

I get the following xml from a web service which I want to convert into .net objects:

let xmlString = "<?xml version=\"1.0\"?>
<logInResponse>
  <result>OK</result>
  <xmlLogIn>
    <session>11C0ED6F8F7288855FC73C99979A9732.TOGKE6VP9aE8abcdLXaVXg</session>
    <user>WallStreetScumbag1</user>
    <authorizations>
      <insiderTrading>true</insiderTrading>
    </authorizations>
  </xmlLogIn>
</logInResponse>"

This is what I have so far:

type Result(value:XmlNode) = 
    member this.Value = value.InnerText

let (|Node|Result|) (node : #System.Xml.XmlNode) =
    if node.Name = "result" then
        Result (new Result(node))
    else
        Node (seq {for x in node.ChildNodes -> x})

let extract node =
    let rec extract node =
        match node with 
        | Result(p) ->
            Seq.singleton p
        | Node(nodes) ->
            Seq.collect (fun (n) -> extract n) nodes
    extract node

let xmlDoc = new XmlDocument()
xmlDoc.LoadXml(xmlString)

//type LogIn() =
    //This is where I would create a .net object that mirrors the xml.

As you can see some elements are repeated within different elements and sometimes elements don't include all the elements that can show up as you can see in my sample xml. This same web service will also reuse many of these same elements in other types of responses. Is it possible to use active patterns to create a general way to deserialize the xml I get back into objects? It appears that this web service never uses attributes to send information, it all seems to be elements inside elements which might make this problem easier. If I am going about this the wrong way please feel free to suggest something better.

Thanks in advance,

Bob

Your approach to use active patterns to match different types of nodes looks good to me. I would probably use partial patterns (that can either return Some when they match or None ) instead of complete patterns (that always return one of the cases). This allows you to write several patterns for different types of nodes you're handling.

let (|Result|_|) (node:#System.Xml.XmlNode) =
  if node.Name = "result" then
    Some(new Result(node))
  else
    None

In the pattern matching, you can then include as many partial patterns as you like and cover all remaining cases using _ (wildcard pattern):

match node with 
| Result(p) ->
    Seq.singleton p
| nd ->
    Seq.collect (fun (n) -> extract n) nd.ChildNodes

Other than this, I'm not sure - the structure in your example looks quite regular, so you don't need to handle cases as in HTML (where <a> can be nested in various other elements). It seems you can just represent the main part of XML as a list of accounts (and parse all account properties using active patterns).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM