• This isn't precisely what you're trying to do, but I found it helpful when trying to do some similar parsing. There's a lot you have to look out for.

    http://www.informit.com/articles/article.aspx?p=25049