Advertisement
Guest User

Untitled

a guest
Apr 25th, 2017
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.69 KB | None | 0 0
  1. #r "System.Xml.Linq.dll"
  2.  
  3. open System.Xml.Linq
  4.  
  5. type Contents =
  6. | Children of Element seq
  7. | Value of string
  8. and Element = {
  9. Name: string
  10. Contents: Contents }
  11.  
  12. let parse xml =
  13. let rec parse (xmlEle: XElement) =
  14. let name = xmlEle.Name.LocalName
  15. let contents =
  16. match xmlEle.HasElements with
  17. | false -> Value xmlEle.Value
  18. | true -> xmlEle.Elements() |> Seq.map parse |> Children
  19. { Name = name; Contents = contents }
  20. XDocument.Parse(xml).Elements() |> Seq.exactlyOne |> parse
  21.  
  22. let unitTest() =
  23. let abba = parse "<a> <b>hi</b> </a>"
  24. match abba.Contents with | Children seq -> seq |> Seq.exactlyOne |> fun x -> x.Contents
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement