Friday, July 13, 2007

Interactive exploration of .NET XML programing with F#

In preparing to upload content into the blog, I ran into a lot problems in properly formatting the content. I have code content that I want to upload into the blog with the color syntax highlighting maintained. It might be that I just don't have the appropriate tools or know the appropriate tools, but it's been painful to get it right. I've must of tried posting this blog entry dozens of time and deleting it in frustration because the content did not come out correctly. I probably could have hand written the blog in html but it was too much manual work and it's not a repeatable process. So I look for ways to simplify this process. I normally write my blog content in Microsoft Word and then export it as html. I wanted to write a script that automatically clean up this exported Microsoft Word document into a form suitable for blog upload. Since my latest interest is in the area of functional programming, I decided to take a crack at this using F#. To my delight, I found that the F# Interactive mode (fsi) provides a great interactive environment for exploring .NET classes. Below is a transcript of how I used F# interactive shell to test out .NET classes:

  1. Starting the shell gives you the following:
    MSR F# Interactive, (c) Microsoft Corporation, All Rights Reserved
    F# Version, compiling for .NET Framework Version v2.0.50727
    NOTE: See 'fsi --help' for flags
    NOTE: Commands: #r ;;    reference (dynamically load) the given DLL.
    NOTE:           #I ;;    add the given search path for referenced DLLs.
    NOTE:           #use ;;  accept input from the given file.
    NOTE:           #load  ...;;
    NOTE:                            load the given file(s) as a compilation unit.
    NOTE:           #time;;          toggle timing on/off.
    NOTE:           #types;;         toggle display of types on/off.
    NOTE:           #quit;;          exit.
    NOTE: Visit the F# website at
    NOTE: Bug reports to Enjoy!
  3. Begin by importing a few critical namespaces
  4. > open System.IO;;
    > open System.Xml;;
  5. Load in the XML document to begin experimentation
  6. > let infile =  "test.htm";;
    val infile : string
    > let xmldoc = new XmlDocument();;
    val xmldoc : XmlDocument
    > xmldoc.Load(infile);;
    val it : unit = ()
  7. Test searching out all <span> elements
  8. > let spanlist = xmldoc.GetElementsByTagName("span");;
    val spanlist : XmlNodeList
  9. Verify that we got something back from the document
  10. > spanlist.Count;;
    val it : int = 278

    Indeed, we got back 278 <span> elements from this xml document. Let's trying examining one of these elements. Let's take a look at the 200th element.

    > spanlist.Item(200).InnerXml;;
    val it : string
    = "<span style=\"mso-spacerun:yes\" xmlns=\"\"></span>"

Notice how I did not need to go through the code/compile/debug cycle to test things out. The entire .NET class framework is available for scripting. I hope this little example demonstrated the utility of the F# interactive framework. Of course, to use this requires that you learn F#, which is modified OCaml. I guess I'm just geeky enough to want to learn new languages so that this is not a hinderance for me. For more information on F#, check out this site :

Okay, I need go back and finish the F# script to help me post my original blog entry that I'm working on.

1 comment:

digital signature PDF said...

Excellent! Thanks for this - I've been looking at this feature for ages. Followed your instructions and it works a treat!