Well the LINQ to XML dom is much more functional by itself so you will get more functional code by using it.

The problem you have above is on this line:

1
let [| titles; authors; isbns |] = _xmlNodes |> Array.map (fun q -> books.SelectNodes(q))

What you are doing is defining three new values (titles, authors, isbns) and using Pattern Matching to assign them. Basically you are telling the F# compiler, what is on the right hand side will have a form where it's an array of elements of the same types and you want it to take the three values of that array and put them in the declarations on the left. The problem is that the F# compiler has no way at compile time to assert that the array on the right will indeed have 3 values in it. It could have 0, 1 or even a hundred and it's telling you what you're doing is risky as it might crash at run time.

To solve the problem you'd need the size to be fixed at compile time.

Instead of using an array to store the node names you could keep the tuple but define a map function over it.

1
2
3
4
5
let mapTuple f (a, b, c) = (f a, f b, f c)

let xmlNodes = ("title", "author", "isbn") |> tupleMap (fun x -> "//books/" + x)

let (titles, authors, isbns) = xmlNodes |> tupleMap (fun q -> books.SelectNodes(q))

Good luck.

By on 6/21/2011 2:08 PM ()

let (titles, authors, isbns) = xmlNodes |> tupleMap (fun q -> books.SelectNodes(q))

Note that you should be able to simplify the above like so:

1
let (titles, authors, isbns) = xmlNodes |> tupleMap books.SelectNodes
By on 6/21/2011 2:10 PM ()

Hi:
Thank you very much, your code works.
By the way, I understand why compiler gave the warning, but I just don't know how to solve it.
But your code works great!
Thanks again and have a nice day!

By on 6/21/2011 9:13 PM ()

How about using the LinqToXML provider instead of the old .NET 2.0 DOM?

I think there are a few examples on fssnip.net but haven't looked into it. I certainly have seen 1 or 2 blog posts on the subject.

By on 6/21/2011 9:54 AM ()

Hello:
Thank you for your remind. I think fssnip.net is really a good web site where people can find quite a lot of useful code snippets.
However, my question is a little special, at least until now I can not find any corresponding code snippet to solve my issue.
But thank you very much for your kind help!

By on 6/21/2011 12:25 PM ()

Hi,

if you add this line:

1
2
3
4
 

#nowarn 25

the warning will be disabled for the current file (but not for the whole project).

By on 6/19/2011 10:28 AM ()

Hi:
Thank you for your idea, but I don't like this way. It is better to use some code, not those compiler switch to reach my goal.
But thanks any way!

By on 6/19/2011 12:42 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