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.
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"> <modifiedDate>3/5/2006</modifiedDate> </book>
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
XML element content
Object or array of Object
XML element attribute name
Error, unless the attribute value is also Nothing
XML element attribute value
Attribute declaration ignored
XML element attribute
XML document root element
Example of an embedded expression in an XML element name:
Example of an embedded expression in the content of an XML element:
Example of an embedded expression in an XML element attribute name:
Example of an embedded expression in an XML element attribute value:
Example of an embedded expression in an XML element attribute:
Example of an embedded expression in an XML document root element:
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.
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.