Let's use XHTML and XmlTextWriter

On Mon, 2004-02-16 at 09:52, Nat Friedman wrote:
> > You can also download the code from here:
> >   http://www.geocities.com/godbyk/MailMessageMatchRenderer.cs.txt
> The code is perfect.

I'm glad to see it, but it's not entirely perfect.  It won't do good
things if there's a "&" character in the subject, for instance.  Also I
reckon that it's the responsibility of the engine to do the two line
breaks after, not the renderer itself.

Dare I suggest that we settle on XHTML 1.0 Transitional for the markup? 
It's future-proof and helps debugging wacked code.  It's also hardly

XHTML's pretty easy to grok.  There are a few rules: all tags are lower
case, all attributes are quoted, and all opening tags have corresponding
closing tags.

<br> --> <br /> (written with the space to avoid confusing old browsers,
now by convention)
<img height=32 src="foo.png" > --> <img height="32" src="foo.png" />

Full details of the changes and advantages in XHTML can be read at

Also and more importantly for us, as it's well-formed XML it doesn't
need to be written with hokey string concatenation and you won't need to
worry about escaping all strings to avoid things like & causing issues.

Instead you use System.Xml.XmlTextWriter.  I attach an example that'll
compile and run, and makes nice tidy output.  You can imagine it would
be easy enough to wrap this a bit more for convenience.

You can read more about this at

I strongly recommend this is the way that renderers are written.  It'll
save us some debugging stress in the future.

-- Edd

using System;
using System.IO;
using System.Xml;

public class WriterTest
	static void Main ()
		StringWriter sw = new StringWriter ();
		XmlWriter xw = new XmlTextWriter (sw);

		// first, write a <div> to enclose everything
		// it's not well-formed XML unless there's one
		// and only one outer element
		xw.WriteStartElement ("div");

		// the title
		xw.WriteElementString ("u", "Email messages");

		// the table
		xw.WriteStartElement ("table");
		xw.WriteAttributeString ("border", "0");
		xw.WriteAttributeString ("cellpadding", "0");
		xw.WriteAttributeString ("cellspacing", "0");
		xw.WriteAttributeString ("width", "100%");

		// here's where you'd do 
		//   foreach (Match m in matches) ....
		// now, time to close everything off
		xw.WriteEndElement (); // table
		xw.WriteEndElement (); // div
		// close the document
		// if we made a mistake in matching the tags, an exception
		// would be thrown here to let us know
		xw.Close ();

		// now, write out the string
		Console.WriteLine (sw.ToString ());

[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]