[...] I can't figure out a clean way, given the root node "A" to disregard any child elements until one gets to a "Deals"

This following works, but I'm sure there's something cleaner (more functional!)

Hey Ronnie, give this a try:

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
40
41
42
43
44
#light
open System.Xml;

type DObject( xml : XmlNode ) =
    let foo = Int32.of_string (xml.Attributes.["foo"]).Value
    let bar = Int32.of_string (xml.Attributes.["bar"]).Value
    member x.foo = foo
    member x.bar = bar

let (|Element|_|) name (inp:#XmlNode) =
    if inp.Name = name
    then Some(inp)
    else None
    
let (|Deal|) (inp:#XmlNode) =
    match inp with
    | Element "Deal" inp -> Some(DObject(inp))
    | _ -> None

let xml = @"<A>
               <B>
                   <C>
                      <Deal foo='1' bar='2'/>
                   </C>
                   <C>
                          <Deal foo='1' bar='2'/>
                   </C>
                   <C>
                        <Deal foo='1' bar='2'/>
                   </C>
                </B>
            </A>"

let dom = System.Xml.XmlDocument()
do dom.LoadXml( xml );
let deals = { for deal in dom.GetElementsByTagName("Deal") -> deal }
            |> Seq.choose (function node -> match node with Deal d -> d )

deals
|> Seq.iter (fun deal ->
    printfn "Deal foo=%i bar=%i" (deal.foo) (deal.bar)
    )

By on 6/9/2008 7:05 PM ()
IntelliFactory Offices Copyright (c) 2011-2012 IntelliFactory. All rights reserved.
Home | Products | Consulting | Trainings | Blogs | Jobs | Contact Us | Terms of Use | Privacy Policy | Cookie Policy
Built with WebSharper