XML Literals: Creating an XML File
One of my favorite features in VB 9 (Visual Studio 2008) is XML Literals. This example demonstrates how insanely easy it is to build an XML file using VB.
This code builds XML from a list of customers.
Dim customerXml As XElement = _
<customers>
<%= From c In custList _
Select <customer>
<LastName><%= c.LastName %></LastName>
<FirstName><%= c.FirstName %></FirstName>
</customer> %>
</customers>
This code starts with the customers root node. It then builds a customer node for each customer found in the list.
This code then saves the XML to the defined file:
customerXml.Save("customers.xml")
NOTE: The definition of the Customer class and the list of customers (custList) used in the above example can be found here.
The resulting XML:
<customers>
<customer>
<LastName>Baggins</LastName>
<FirstName>Billbo</FirstName>
</customer>
<customer>
<LastName>Baggins</LastName>
<FirstName>Frodo</FirstName>
</customer>
<customer>
<LastName>Kurata</LastName>
<FirstName>Deborah</FirstName>
</customer>
</customers>
Enjoy!
P.S. In a later post here, I show how to read this XML file and reconstitute the list of customers.
Miller_a — July 17, 2009 @ 1:59 am
Hi Deborah,
I was directed here by you to look at this, does this work on .net framework 2.0 with visual studo 2005?
DeborahK — July 17, 2009 @ 10:36 am
Hi Miller –
No. As it states at the top of the post, this is for Visual Studio 2008, .NET Framework 3.5.
(Guess I should have asked that before linking you to this page.)
Jerry T. — October 9, 2009 @ 8:23 am
This looks very simple. Is this an aspect of LINQ or just part of VB.NET itself now?
ALSO, how would one add an attribute to the Customer element such that it read like this:
DeborahK — October 9, 2009 @ 5:59 pm
Hi Jerry –
Thank you for coming by the blog. XML Literals are a feature of VB.NET only.
To add an attribute, the code would look something like this:
Dim customerXml As XElement = _
>
<%= c.LastName %>
<%= c.FirstName %>
%>
<%= From c In custList _ Select
Hope this helps.
Emmanuel Huna — February 19, 2010 @ 4:10 pm
It seems that VB 9’s XML literals are not properly escaping single quotes, double quotes and carriage return/line feed.
It is properly escaping less than, greater than and ampersand.
I can escape everything properly myself – here’s one way:
sReturn = sString.Replace(“&”, “&”).Replace(“<", "<").Replace(">“, “>”).Replace(Chr(34), “"”).Replace(“‘”, “'”)
But VB’s XML literals are now escaping my ampersands twice. How can I tell the xml literals not to escape the characters?
DeborahK — February 19, 2010 @ 8:17 pm
Hi Emmanuel –
See this blog post:
http://msmvps.com/blogs/deborahk/archive/2010/02/19/xml-literals-escaping-characters.aspx
Hope this helps.
Art Colman — March 25, 2010 @ 9:51 am
I’m having difficulty determining how to handle optional element creation using XML literals. I’m looking to code similar to below where is only added to if it exists. Any thoughts?
<%= If(productID.Length > 0,
I get an error that the compiler cannot infer a common type for the second and third operands.
DeborahK — March 25, 2010 @ 12:41 pm
Hi Art –
I added a blog post about this topic here: http://msmvps.com/blogs/deborahk/archive/2010/03/25/xml-literals-handling-empty-elements.aspx
Hope this helps.