LINQ into XML

To select data into XML objects, you can use syntax similar to the syntax you use to build an XML literal. You then add the special characters <%= ... %> to indicate a “hole” within the literal. Inside the hole, you replace the ellipsis with a LINQ query that extracts data from Visual Basic objects and uses them to build new XML objects.

For example, suppose the all_customers list contains Customer objects. The following code builds an XElement object that contains Customer XML elements for all of the Customer objects:

Dim x_all As XElement = _
    <AllCustomers>
        <%= From cust In all_customers
            Select New XElement("Customer",
            New XAttribute("FirstName", cust.FirstName),
            New XAttribute("LastName", cust.LastName),
            New XText(cust.Balance.ToString("0.00")))
        %>
    </AllCustomers>

The following text shows a sample of the resulting XML element:

<AllCustomers>
  <Customer FirstName="Ann" LastName="Archer">100.00</Customer>
  <Customer FirstName="Ben" LastName="Best">-24.54</Customer>
  <Customer FirstName="Carly" LastName="Cant">62.40</Customer>
</AllCustomers>

You can have more than one hole in the XML literal. The following code uses an XML literal that contains two holes. The first uses a Where clause to select customers with non-negative balances, and the second selects customers with negative balances. It places these two groups of customers inside different sub-elements within the resulting XML.

' Separate customers with positive and negative balances. Dim separated As XElement = ...

Get Visual Basic 2012 Programmer's Reference now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.