XML Literals: Creating an XML File

Posted Thu, Jul 2 2009 17:36 by Deborah Kurata

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.

Filed under: , , ,

Comments

# XML Literals: Reading an XML File

Wednesday, July 08, 2009 4:28 PM by Deborah's Developer MindScape

In a prior post here , I created an XML file using VB 9 (Visual Basic 2008/.NET Framework 3.5). This

# re: XML Literals: Creating an XML File

Friday, July 17, 2009 1:59 AM by Miller_a

Hi Deborah,

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

# re: XML Literals: Creating an XML File

Friday, July 17, 2009 10:36 AM by Deborah Kurata

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

# Formatting Text Files

Tuesday, July 21, 2009 3:16 PM by Deborah's Developer MindScape

There are often times that you need to write out text files containing data managed by your application

# But what about attributes?

Friday, October 09, 2009 8:23 AM by Jerry T.

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:

<Customer id="12345"> in the resulting XML

# re: XML Literals: Creating an XML File

Friday, October 09, 2009 5:59 PM by Deborah Kurata

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

<customers>

  <%= From c In custList _

      Select <customer id=<%= c.CustomerId %>>

                <LastName><%= c.LastName %></LastName>

                <FirstName><%= c.FirstName %></FirstName>

             </customer> %>

</customers>

Hope this helps.

Leave a Comment

(required) 
(required) 
(optional)
(required)