Posted by : Michael Mortensen Friday, June 7, 2013

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:
public Stream GetExmapleXml()
{
    Stream output = null;
    MemoryStream tempOutput = null;
    try
    {
        tempOutput = new MemoryStream();
        using (XmlWriter writer = XmlWriter.Create(tempOutput))
        {
            // build your XML here
            writer.WriteStartElement("Xml");
            writer.WriteString("A very simple XML representation - and yes, I could just have used writer.WriteElementString(..) - but hey - it's my example :-)");
            writer.WriteEndDocument();
            writer.Flush();
        }
        tempOutput.Flush();
        tempOutput.Position = 0;
        output = tempOutput;
        tempOutput = null;
    }
    finally
    {
        if (tempOutput != null) { tempOutput.Dispose(); }
    }
    return output;
}
Figure 1: A more common way of writing XML.

This would return a Stream holding this XML:
<?xml version="1.0" encoding="UTF-8"?>
<Xml>A very simple XML representation - and yes, I could just have used writer.WriteElementString(..) - but hey - it's my example :-)</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 Action delegate, which has the same characteristica as the one 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:


public Stream GetExmapleXml()
{
    return XmlWriterUtility.CreateXml(GetExmapleXmlImpl);
}

public void GetExmapleXmlImpl(XmlWriter writer)
{
    writer.WriteStartElement("Xml");
    writer.WriteString("A very simple XML representation - and yes, I could just have used writer.WriteElementString(..) - but hey - it's my example :-)");
    writer.WriteEndDocument();
}
Figure 3: My preferred way of using the implementation - but hey - I am dinosaur in this fancy Lambda world.


public Stream GetExmapleXmlDelegate()
{
    return XmlWriterUtility.CreateXml(delegate(XmlWriter writer)
                                          {
                                              writer.WriteStartElement("Xml");
                                              writer.WriteString("A very simple XML representation - and yes, I could just have used writer.WriteElementString(..) - but hey - it's my example :-)");
                                              writer.WriteEndDocument();
                                          });
}
Figure 4: Same result as in Figure 3 - just using an anonymous method.


public Stream GetExmapleXmlLambda()
{
    return XmlWriterUtility.CreateXml(writer =>
        {
            writer.WriteStartElement("Xml");
            writer.WriteString("A very simple XML representation - and yes, I could just have used writer.WriteElementString(..) - but hey - it's my example :-)");
            writer.WriteEndDocument();
        });
}
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:


ConvertUtility.ToString(GetExmapleXml());

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.

Leave a Reply

Subscribe to Posts | Subscribe to Comments

Popular Post

© 2008-2013 MuchMore. All rights reserved.. Powered by Blogger.

© 2008-2013 MuchMore. All rights reserved.
Metrominimalist
Powered by Blogger
Designed by Johanes Djogan