Well, it's no secret - I am a big fan of XML and the possibilities it brings to a digital connected world. Yes, it may be fat - but if the consuming client support GZIP or similar - that is a big fat-loss. Still not convinced? Well, you can always transform it to a more lightweight candidate, such as JSON. That's how versatile XML is.
Speaking of versatile, there are many ways to create XML in the .NET Framework. Ranging from ways of a simple string to a StringBuilder to XmlDocument to XElement to XmlWriter to many, many more. Just Google xml .net followed by your favorite syntax, eg. c#, and several millions result is served to you.
Personally, I have always favored the XmlWriter approach, as it is as close to the core as it gets without overhead of the more popular XElement and the likes.
A typical way to use the aforementioned XmlWriter could be like this:
Figure 1: A more common way of writing XML.
This would return a Stream holding this XML:
Figure 2: The XML output as produced by Figure 1, Figure 3, Figure 4 and Figure 5.
Although very performance friendly and to some extent easy on the eye, I decided to provide a more generic and simple way of achieving the same using the Cuemon.Xml assembly. In this assembly you will find the XmlWriterUtility class that (in the writing of this entry) consist of two methods; CreateSettings and CreateXml.
Since my primary goal is to be backward compatible with .NET 2.0 SP1, the CreateXml method can take up to 10 arguments using an Act delegate, which has the same characteristica as the Action delegate found in .NET 4.0. The default XML settings (should you choose not to provide a XmlWriterSettings) are the one found in the parameter less CreateSettings method.
Anyway, have a look at the same example as above - just the Cuemon way in three different forms:
Figure 3: My preferred way of using the implementation - but hey - I am dinosaur in this fancy Lambda world.
Figure 4: Same result as in Figure 3 - just using an anonymous method.
Figure 5: Same result as in Figure 3 and 4 - just using lamda expression as supported by .NET 3.0.
There you have it - a smart and easy way of creating versatile XML that can be used in all sorts of scenarios. Want a string representation of the Stream? You asked for it - just use the ConvertUtility.ToString(..) like this:
I hope you enjoyed this little introduction to the XmlWriterUtility class - and can see and unleash the potential of it. As always - happy coding - and thanks for stopping by.