XML Element Literal (Visual Basic)

A literal that represents an XElement object.

<name [ attributeList ] />
-or-
<name [ attributeList ] > [ elementContents ] </[ name ]>

Parts

Term

Definition

<

Required. Opens the starting element tag.

name

Required. Name of the element. The format is one of the following:

  • Literal text for the element name, of the form [ePrefix:]eName, where:

Part

Description

ePrefix

Optional. XML namespace prefix for the element. Must be a global XML namespace that is defined with an Imports statement in the file or at the project level, or a local XML namespace that is defined in this element or a parent element.

eName

Required. Name of the element. The format is one of the following:

  • Embedded expression of the form <%= nameExp %>. The type of nameExp must be String or a type implicitly convertible to XName. An embedded expression is not allowed in a closing tag of an element.

attributeList

Optional. List of attributes declared in the literal.

attribute [ attribute ... ]

Each attribute has one of the following syntaxes:

  • Attribute assignment, of the form [aPrefix:]aName=aValue, where:

Part

Description

aPrefix

Optional. XML namespace prefix for the attribute. Must be a global XML namespace that is defined with an Imports statement, or a local XML namespace that is defined in this element or a parent element.

aName

Required. Name of the attribute. The format is one of the following:

aValue

Optional. Value of the attribute. The format is one of the following:

  • Literal text, enclosed in quotation marks.

  • Embedded expression of the form <%= aValueExp %>. Any type is allowed.

  • Embedded expression of the form <%= aExp %>.

/>

Optional. Indicates that the element is an empty element, without content.

>

Required. Ends the beginning or empty element tag.

elementContents

Optional. Content of the element.

content [ content ... ]

Each content can be one of the following:

</[name]>

Optional. Represents the closing tag for the element. The optional name parameter is not allowed when it is the result of an embedded expression.

Return Value

An XElement object.

Remarks

You can use the XML element literal syntax to create XElement objects in your code.

Note

An XML literal can span multiple lines without using line continuation characters. This feature enables you to copy content from an XML document and paste it directly into a Visual Basic program.

Embedded expressions of the form <%= exp %> enable you to add dynamic information to an XML element literal. For more information, see Embedded Expressions in XML (Visual Basic).

The Visual Basic compiler converts the XML element literal into calls to the XElement constructor and, if it is required, the XAttribute constructor.

XML Namespaces

XML namespace prefixes are useful when you have to create XML literals with elements from the same namespace many times in code. You can use global XML namespace prefixes, which you define by using the Imports statement, or local prefixes, which you define by using the xmlns:xmlPrefix="xmlNamespace" attribute syntax. For more information, see Imports Statement (XML Namespace).

In accordance with the scoping rules for XML namespaces, local prefixes take precedence over global prefixes. However, if an XML literal defines an XML namespace, that namespace is not available to expressions that appear in an embedded expression. The embedded expression can access only the global XML namespace.

The Visual Basic compiler converts each global XML namespace that is used by an XML literal into a one local namespace definition in the generated code. Global XML namespaces that are not used do not appear in the generated code.

Example

The following example shows how to create a simple XML element that has two nested empty elements.

Dim test1 As XElement = 
<outer>
    <inner1></inner1>
    <inner2/>
</outer>

Console.WriteLine(test1)

The example displays the following text. Notice that the literal preserves the structure of the empty elements.

<outer>
  <inner1></inner1>
  <inner2 />
</outer>

The following example shows how to use embedded expressions to name an element and create attributes.

Dim elementType = "book" 
Dim authorName = "My Author" 
Dim attributeName1 = "year" 
Dim attributeValue1 = 1999
Dim attributeName2 = "title" 
Dim attributeValue2 = "My Book" 

Dim book As XElement = 
<<%= elementType %>
    isbn="1234"
    author=<%= authorName %>
    <%= attributeName1 %>=<%= attributeValue1 %>
    <%= New XAttribute(attributeName2, attributeValue2) %>
/>

Console.WriteLine(book)

This code displays the following text:

<book isbn="1234" author="My Author" year="1999" title="My Book" />

The following example declares ns as an XML namespace prefix. It then uses the prefix of the namespace to create an XML literal and displays the element's final form.

' Place Imports statements at the top of your program.   
Imports <xmlns:ns="http://SomeNamespace">

Class TestClass1

    Shared Sub TestPrefix()
        ' Create test using a global XML namespace prefix.  
        Dim inner2 = <ns:inner2/>

        Dim test = 
        <ns:outer>
            <ns:middle xmlns:ns="http://NewNamespace">
                <ns:inner1/>
                <%= inner2 %>
            </ns:middle>
        </ns:outer>

        ' Display test to see its final form. 
        Console.WriteLine(test)
    End Sub 

End Class

This code displays the following text:

<ns:outer xmlns:ns="http://SomeNamespace">
  <ns:middle xmlns:ns="http://NewNamespace">
    <ns:inner1 />
    <inner2 xmlns="http://SomeNamespace" />
  </ns:middle>
</ns:outer>

Notice that the compiler converted the prefix of the global XML namespace into a prefix definition for the XML namespace. The <ns:middle> element redefines the XML namespace prefix for the <ns:inner1> element. However, the <ns:inner2> element uses the namespace defined by the Imports statement.

See Also

Reference

XElement

XML Comment Literal (Visual Basic)

XML CDATA Literal (Visual Basic)

Imports Statement (XML Namespace)

Concepts

Names of Declared XML Elements and Attributes (Visual Basic)

Embedded Expressions in XML (Visual Basic)

Other Resources

XML Literals (Visual Basic)

Creating XML in Visual Basic