Embedded Expressions in XML (Visual Basic)

Embedded expressions enable you to create XML literals that contain expressions that are evaluated at run time. The syntax for an embedded expression is <%= expression %>, which is the same as the syntax used in ASP.NET.

For example, you can create an XML element literal, combining embedded expressions with literal text content.

Dim isbnNumber As String = "12345"
Dim modifiedDate As String = "3/5/2006"
Dim book As XElement = 
    <book category="fiction" isbn=<%= isbnNumber %>>
        <modifiedDate><%= modifiedDate %></modifiedDate>

If isbnNumber contains the integer 12345 and modifiedDate contains the date 3/5/2006, when this code executes, the value of book is:

<book category="fiction" isbn="12345">  

Embedded Expression Location and Validation

Embedded expressions can appear only at certain locations within XML literal expressions. The expression location controls which types the expression can return and how Nothing is handled. The following table describes the allowed locations and types of embedded expressions.

Location in literal Type of expression Handling of Nothing
XML element name XName Error
XML element content Object or array of Object Ignored
XML element attribute name XName Error, unless the attribute value is also Nothing
XML element attribute value Object Attribute declaration ignored
XML element attribute XAttribute or a collection of XAttribute Ignored
XML document root element XElement or a collection of one XElement object and an arbitrary number of XProcessingInstruction and XComment objects Ignored
  • Example of an embedded expression in an XML element name:

    Dim elementName As String = "contact"
    Dim contact1 As XElement = <<%= elementName %>/>
  • Example of an embedded expression in the content of an XML element:

    Dim contactName As String = "Patrick Hines"
    Dim contact2 As XElement = 
      <contact><%= contactName %></contact>
  • Example of an embedded expression in an XML element attribute name:

    Dim phoneType As String = "home"
    Dim contact3 As XElement = 
      <contact <%= phoneType %>="206-555-0144"/>
  • Example of an embedded expression in an XML element attribute value:

    Dim phoneNumber As String = "206-555-0144"
    Dim contact4 As XElement = 
      <contact home=<%= phoneNumber %>/>
  • Example of an embedded expression in an XML element attribute:

    Dim phoneAttribute As XAttribute = 
      New XAttribute(XName.Get(phoneType), phoneNumber)
    Dim contact5 As XElement = 
      <contact <%= phoneAttribute %>/>
  • Example of an embedded expression in an XML document root element:

    Dim document As XDocument = 
      <?xml version="1.0"?><%= contact1 %>

If you enable Option Strict, the compiler checks that the type of each embedded expression widens to the required type. The only exception is for the root element of an XML document, which is verified when the code runs. If you compile without Option Strict, you can embed expressions of type Object and their type is verified at run time.

In locations where content is optional, embedded expressions that contain Nothing are ignored. This means you do not have to check that element content, attribute values, and array elements are not Nothing before you use an XML literal. Required values, such as element and attribute names, cannot be Nothing.

For more information about using an embedded expression in a particular type of literal, see XML Document Literal, XML Element Literal.

Scoping Rules

The compiler converts each XML literal into a constructor call for the appropriate literal type. The literal content and embedded expressions in an XML literal are passed as arguments to the constructor. This means that all Visual Basic programming elements available to an XML literal are also available to its embedded expressions.

Within an XML literal, you can access the XML namespace prefixes declared with the Imports statement. You can declare a new XML namespace prefix, or shadow an existing XML namespace prefix, in an element by using the xmlns attribute. The new namespace is available to the child nodes of that element, but not to XML literals in embedded expressions.


When you declare an XML namespace prefix by using the xmlns namespace attribute, the attribute value must be a constant string. In this regard, using the xmlns attribute is like using the Imports statement to declare an XML namespace. You cannot use an embedded expression to specify the XML namespace value.

See also