-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathXmlPickler.Sequence.fsx
40 lines (32 loc) · 1.09 KB
/
XmlPickler.Sequence.fsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
module XmlPickler.Sequence
open System.Xml
open System.IO
type XmlAttr = { Name: string; Value: string }
type XmlElem =
| Element of Name: string * Attributes: XmlAttr list
| EmptyElement of Name: string * Attributes: XmlAttr list
| Text of string
| EndElement of string
let isElement = function Element _ -> true | _ -> false
let isEmptyElement = function EmptyElement _ -> true | _ -> false
let isText = function Text _ -> true | _ -> false
let isEndElement = function EndElement _ -> true | _ -> false
module Implementation =
let createEl (reader: XmlTextReader) =
match reader.NodeType with
| XmlNodeType.Element ->
if reader.IsEmptyElement then
EmptyElement (reader.Name, []) |> Some
else
Element (reader.Name, []) |> Some
| XmlNodeType.Text -> reader.Value |> Text |> Some
| XmlNodeType.EndElement -> reader.Name |> EndElement |> Some
| _ -> None
open Implementation
let xmlReaderToSeq (reader: XmlTextReader) =
seq {
while reader.Read () do
let el = createEl reader
if (el.IsSome) then
yield el.Value
}