More Ways to create an XML file by ThinqLinq

More Ways to create an XML file

Edward Tanguay just posted three comparative examples of creating XML files via String Concatenation, XmlWriter, and LINQ to XML. I would typically respond to a blog post through that site's comments, but public comments are blocked at this point. While his post is a good start and shows pretty easily the simplicity that using LINQ to XML gives over the other options, he does fail to address a couple other options:

  • XmlDocument
  • Serialized objects, and of course
  • XML Literals with VB.

Before discussing each of these options, I want to take a minute to address the real issues with the string concatenation sample. While it is relatively easy to use string concatenation to build up the XML, it can be quite dangerous. The main issue is the ease at which you can add invalid characters into the XML elements. In particular, if your variable has one of the characters that need to be escaped (like & or <), using the sample code will cause invalid XML to be generated. You have to properly escape all of the variables which can quickly make the code much more complex. Luckily each of the other XML options handle escaping these values correctly for you automatically.

On to the other options. While you could use the XmlDocument rather than XmlWriter, coding it is much more complex. Also, it doesn't allow for streaming the results out which causes a higher memory footprint as the entire document will be in memory.

Serialized objects give a nice type-safe option, but they do require you to code the object structures before you can consume them. Using tools like XSD.exe or LINQ to XSD helps reduce this overhead, but it is something to be aware of.

I would be remiss if I didn't offer a XML Literal option, so here you go. Here's Edward's C# Sample for reference purposes:

 
XDocument doc = new XDocument(
                new XDeclaration("1.0", null, null),
                new XElement(_pluralCamelNotation,
                    Enumerable.Range(1, 3).Select(
                        i => new XElement(_singularCamelNotation,
                            _allDataTypes.Select(
                                dataType => new XElement(
                                    dataType.CamelCaseNotation,
                                    dataType.GetDummyData())
                            )
                    ))));
            return doc.ToString();

And here's the VB equivalent using XML Literals:


Dim doc = <?xml version="1.0"?>
          <<%= _pluralCamelNotation %>>
              <%= From i In Enumerable.Range(1, 3) _
                  Select <<%= _singluarNotation %>>
                           <%= From t In _allDataTypes _
                               Select <<%= t.CamelCaseNotation %>>
                                           <%= t.GetDummyData %></> %>
                           </> %>
               </>
As it is, we are able to drop a couple lines from the C# code. However, this example is a bit less consise as some XML Literal examples because we are not only inserting the element's values, but the names of the elements themselves dynamically through the literal. As a result there are a number of cases where we have double opening XML tags (<<) which detracts from the readability somewhat. Choosing to use LINQ expressions over Lambda's is purely a matter of style and preference.


        
Posted on - Comment
Categories: Linq to XML -
comments powered by Disqus