Partager via


Introduction to XML Literals in Visual Basic

This section provides information about creating XML trees in Visual Basic.

For information about using the results of LINQ queries as the content for an XML tree, see Functional Construction (LINQ to XML).

For more information on XML literals in Visual Basic, see Overview of LINQ to XML in Visual Basic.

Creating XML Trees

The following example shows how to create an XElement, in this case contacts:

Dim contacts As XElement = _
    <Contacts>
        <Contact>
            <Name>Patrick Hines</Name>
            <Phone>206-555-0144</Phone>
            <Address>
                <Street1>123 Main St</Street1>
                <City>Mercer Island</City>
                <State>WA</State>
                <Postal>68042</Postal>
            </Address>
        </Contact>
    </Contacts>

Creating an XElement with Simple Content

You can create an XElement that contains simple content, as follows:

Dim n as XElement = <Customer>Adventure Works</Customer>
Console.WriteLine(n) 

This example produces the following output:

<Customer>Adventure Works</Customer>

Creating an Empty Element

You can create an empty XElement, as follows:

Dim n As XElement = <Customer/>
Console.WriteLine(n)

This example produces the following output:

<Customer />

Using Embedded Expressions

An important feature of XML literals is that they allow embedded expressions. Embedded expressions enable you to evaluate an expression and insert the results of the expression into the XML tree. If the expression evaluates to a type of XElement, an element is inserted into the tree. If the expression evaluates to a type of XAttribute, an attribute is inserted into the tree. You can insert elements and attributes into the tree only where they are valid.

It is important to note that only a single expression can go into an embedded expression. You cannot embed multiple statements. If an expression extends beyond a single line, you must use the line continuation character.

If you use an embedded expression to add existing nodes (including elements) and attributes to a new XML tree and if the existing nodes are already parented, the nodes are cloned. The newly cloned nodes are attached to the new XML tree. If the existing nodes are not parented, the nodes are simply attached to the new XML tree. The last example in this topic demonstrates this.

The following example uses an embedded expression to insert an element into the tree:

Dim xmlTree1 As XElement = _
    <Root>
        <Child>Contents</Child>
    </Root>
Dim xmlTree2 As XElement = _
    <Root>
        <%= xmlTree1.<Child> %>
    </Root>
Console.WriteLine(xmlTree2)

This example produces the following output:

<Root>
  <Child>Contents</Child>
</Root>

Using Embedded Expressions for Content

You can use an embedded expression to supply the content of an element:

Dim str As String
str = "Some content"
Dim root As XElement = <Root><%= str %></Root>
Console.WriteLine(root)

This example produces the following output:

<Root>Some content</Root>

Using a LINQ Query in an Embedded Expression

You can use the results of a LINQ query for the content of an element:

Dim arr As Integer() = {1, 2, 3}

Dim n As XElement = _
    <Root>
        <%= From i In arr Select <Child><%= i %></Child> %>
    </Root>

Console.WriteLine(n)

This example produces the following output:

<Root>
  <Child>1</Child>
  <Child>2</Child>
  <Child>3</Child>
</Root>

Using Embedded Expressions for Node Names

You can also use embedded expressions to calculate attribute names, attribute values, element names, and element values:

Dim eleName As String = "ele"
Dim attName As String = "att"
Dim attValue As String = "aValue"
Dim eleValue As String = "eValue"
Dim n As XElement = _
    <Root <%= attName %>=<%= attValue %>>
        <<%= eleName %>>
            <%= eleValue %>
        </>
    </Root>
Console.WriteLine(n)

This example produces the following output:

<Root att="aValue">
  <ele>eValue</ele>
</Root>

Cloning vs. Attaching

As mentioned earlier, if you use an embedded expression to add existing nodes (including elements) and attributes to a new XML tree, if the existing nodes are already parented, the nodes are cloned and the newly cloned nodes are attached to the new XML tree. If the existing nodes are not parented, they are simply attached to the new XML tree.

' Create a tree with a child element.
Dim xmlTree1 As XElement = _
    <Root>
        <Child1>1</Child1>
    </Root>

' Create an element that is not parented.
Dim child2 As XElement = <Child2>2</Child2>

' Create a tree and add Child1 and Child2 to it.
Dim xmlTree2 As XElement = _
    <Root>
        <%= xmlTree1.<Child1>(0) %>
        <%= child2 %>
    </Root>

' Compare Child1 identity.
Console.WriteLine("Child1 was {0}", _
    IIf(xmlTree1.Element("Child1") Is xmlTree2.Element("Child1"), _
    "attached", "cloned"))

' Compare Child2 identity.
Console.WriteLine("Child2 was {0}", _
    IIf(child2 Is xmlTree2.Element("Child2"), _
    "attached", "cloned"))

This example produces the following output:

Child1 was cloned
Child2 was attached

See Also

Concepts

Creating XML Trees