These expressions have the following syntax:
<%=expression%>
With Visual Basic you can place embedded expressions directly within your XML literals. These
expressions are then evaluated at runtime.
For example, the following code defines two variables, one that contains the employee ID, and the
other that contains the employee name. These two variables are then used as embedded expressions
in the creation of the XML literal to specify the value of the empID attribute value and the FirstName
element value.
Dim empID As Integer
Dim empName As String
empID = 1
empName = "Scott"
Dim emp As XElement = _
>
<%= empName %>
182
Chapter 9: LINQ to XML and Visual Basic .NET
When this code is run, you??™ll get the following output:
Scott
In this example, embedded expressions were used as an attribute value on the XML element and as XML
element content value. Likewise, you can use embedded expressions as names in the name/value pair,
as shown here:
Dim empID As String
Dim empName As String
empID = "ID"
empName = "FirstName"
Dim emp As XElement = _
="1">
<<%= empName %>/>
When this code runs, you get the following output:
This can be taken one step further by including values for the name/value pair, like this:
Dim empID As String
Dim empName As String
empID = "ID"
empName = "FirstName"
Dim emp As XElement = _
="1">
<<%= empName %>>Scott>
This code produces the following:
Scott
There are a couple of noteworthy items of which you should be aware.
Pages:
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311