Converting XML to RTF using .net
It is quite easy to convert XML into RTF, well to turn it into fairly simple rtf output anyway. The RTF format is pretty straightforward and this makes it pretty easy to write into the RtfTextBox inside the .net system to display your xml in a nicely formatted way. With .net 2.0 it is possible to add a WebBrowser component instead and convert your xml into html.
One thing to be careful of, the transform will only deal with valid xml, so if you are doing a transform on a segment of the code, turn the whole thing into a new document with a XmlDefintion up the top of the file or it will fail to convert at all, while also not telling you what went wrong.
Here is an example of an xslt StyleSheet I used to converting an xml file into output:
<xsl:stylesheet
version="1.0"
xmlns:xsl="https://www.w3.org/1999/XSL/Transform"
xmlns:ToolManTools="urn:ToolManTools"
>
<xsl:output method="text"/>
<xsl:template match="FreeFormData">
<xsl:text>{\rtf1</xsl:text>
<xsl:text>{\info{\title Frog data}{\author David Bennett}}</xsl:text>
<xsl:text>\par\b Created: \b0\tab </xsl:text>
<xsl:value-of select="ToolManTools:GetDateFromTicks(./Created)"/>
<xsl:text>\par </xsl:text>
<xsl:for-each select="Data/Node">
<xsl:text>\b </xsl:text>
<xsl:value-of select="Key" />
<xsl:text>\b0\tab\tab </xsl:text>
<xsl:value-of select="Value" />
<xsl:text>\line </xsl:text>
</xsl:for-each>
<xsl:text>\par </xsl:text>
<xsl:text>}</xsl:text>
</xsl:template>
</xsl:stylesheet>
From this output it is possible to see that to start with you just write out a small rtf header with the {\rtf output. Then I write out some information about the file, giving it a header and an author. The \b turns on bold and \b0 turns it off. \i turns on italic and \i0 turns it off. The \tab command inserts a tab character, and causes the output to be lined up. The \line command puts in a line break at the specified location and \par does a paragraph output.
I also use an external object in this style sheet to convert the ticks stored in the xml file into a useful string output. The object gets passed in using the XsltArgumentList object and is mapped to a specific namespace with the xmlns argument in the xsl:stylesheet element. It is pretty easy to add in, all xml functions are assumed to take in a string and return a string.
XmlDocument doc = new XmlDocument();
XslTransform transform = new XslTransform();
XsltArgumentList args = new XsltArgumentList();
StringWriter str = new StringWriter();
transform.Load(dir + "\\" + this.incidentEvent.Name + ".xslt");
// Put this in, just in case it needs it...
doc.AppendChild(doc.CreateXmlDeclaration("1.0", null, null));
doc.AppendChild(this.incidentEvent.ToXml(doc));
args.AddExtensionObject("urn:ToolManTools", new ToolManTools());
transform.Transform(doc, args, str, null);
tmp = str.ToString();
DataTextBox.Rtf = tmp;
You can see that I add in the extension object there and that I use a StringrWriter to get the RTF before putting into the RtfTextBox for viewing.