Deborah's Developer MindScape






         Tips and Techniques for Web and .NET developers.

July 2, 2009

XML Literals: Creating an XML File

Filed under: LINQ,VB.NET,XML @ 7:36 pm

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.

8 Comments

  1.   Miller_a — July 17, 2009 @ 1:59 am    Reply

    Hi Deborah,

    I was directed here by you to look at this, does this work on .net framework 2.0 with visual studo 2005?

  2.   DeborahK — July 17, 2009 @ 10:36 am    Reply

    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.)

  3.   Jerry T. — October 9, 2009 @ 8:23 am    Reply

    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:

    in the resulting XML

  4.   DeborahK — October 9, 2009 @ 5:59 pm    Reply

    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 = _

    < %= From c In custList _ Select >
    < %= c.LastName %>
    < %= c.FirstName %>
    %>

    Hope this helps.

  5.   Emmanuel Huna — February 19, 2010 @ 4:10 pm    Reply

    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?

  6.   DeborahK — February 19, 2010 @ 8:17 pm    Reply

    Hi Emmanuel –

    See this blog post:

    http://msmvps.com/blogs/deborahk/archive/2010/02/19/xml-literals-escaping-characters.aspx

    Hope this helps.

  7.   Art Colman — March 25, 2010 @ 9:51 am    Reply

    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, < %= productID %>, wasPopulated = False) %>

    I get an error that the compiler cannot infer a common type for the second and third operands.

  8.   DeborahK — March 25, 2010 @ 12:41 pm    Reply

    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.

RSS feed for comments on this post. TrackBack URI

Leave a comment

© 2019 Deborah's Developer MindScape   Provided by WPMU DEV -The WordPress Experts   Hosted by Microsoft MVPs