- Back to Home »
- XML the smart way - and beyond!
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;
}
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>
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();
}
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();
});
}
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();
});
}
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.
