John Lam 1997 — 2026

Fun (sort of) with XML

Like Craig, I’ve been spending some time building an XML authoring system. Yesterday, I wrote a simple WordML compiler that will take an existing document and insert external references into the document. My motivation for doing this was to try and eliminate the inevitable bugs that creep into a manuscript whenever you cut and paste code fragments into the document. What my system lets you do is insert a reference like:

<<\temp\somecodefile.cs>>

into your word document, and my WordML compiler will replace the reference with the contents of somecodefile.cs.

This works fine for my current writing project, “A Busy Developer’s Guide to GSLgen”. However, later on, I’m going to need to insert some code fragments into my Word document. This requires a bit more processing on my part, which I can accomplish with a bit of regular expression work. For example, if I had this reference:

<<\temp\somecodefile.cs#codefrag1>>

It would pull a code fragment that is marked using comments and #region statements, such as:

// #region codefrag1 public void DoSomething() { } // #endregion codefrag1

Since I want this to work with more than one kind of source code file, I’m going to build a plug-in mechanism that allows folks to write their own regular expressions that parse source code files in their favorite programming language.

One other thing: I transform the WordML document in place via XmlDocument. I did this mostly because I was in a hurry and I didn’t want to deal with the pain that is XmlReader/Writer. It would be so much nicer if there was sed-like support for modifying XML streams in the frameworks.

I’ll release the source code in the next couple of days once I’ve had a chance to dogfood it.

2 archived reader comments

Scott Watermasysk (2003-07-03)

Hi John,

Sounds cool. I looked around for something like this a while ago but did not really find anything.

Looking forward to seeing the bit.

-Scott

Alex Peake (2003-07-03)

Take a look at XPathNavigator