Embedded Expressions in XML (Visual Basic)
Updated: July 20, 2015
For the latest documentation on Visual Studio 2017 RC, see Visual Studio 2017 RC Documentation.
Embedded expressions enable you to create XML literals that contain expressions that are evaluated at run time. The syntax for an embedded expression is
%>, 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> </book>
isbnNumber contains the integer 12345 and
modifiedDate contains the date 3/5/2006, when this code executes, the value of
<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 |
|XML element name||XName||Error|
|XML element content||Ignored|
|XML element attribute name||XName||Error, unless the attribute value is also |
|XML element attribute value||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:
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
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