homelinksBlijf op de hoogte van de recente aanpassingen op vbvoorbeelden!
Visual Basic 2010 Voorbeelden

visual basic 2010 broncode voorbeelden

Microsoft Visual Studio 2010Microsoft Developers Network - Visual BasicMicrosoft .NET Framework

28.3. Embedded Expressies in XML Literals

Print Email Deel op Facebook Deel op Twitter

Dit artikel is gepubliceerd op zondag 31 juli 2011 op vbvoorbeelden, bezoek de website voor een recente versie van dit artikel of andere artikels.

In het vorige topic hadden we statische XML literals die altijd naar dezelfde XML data evalueerden.
Een meer dynamisch aanpak bestaat erin de data voor de XML te halen uit run time informatie.  Zowat alles kan dynamisch worden opgehaald, dit kunnen bijvoorbeeld de attribuut of elementen waardes, of namen zijn.

De synthax voor een embedded expressie is iets als : <%= expression %>
De waarde waarnaartoe de expressie evalueert, is ook de waarde waardoor de embedded expressie wordt vervangen.

In onderstaand voorbeeld wordt de embedded expressie <%= employee1.Name %> vervangen door de naam "John Smith" :
Visual Basic 2010 Broncode
Option Explicit On
Option Strict On

Imports System.Xml.Linq

Namespace EmbeddedExpressions
    Class Example1
        Public Shared Sub Main()
            Dim employee1 As New Employee With {.Name = "John Smith"}
            '
            Dim employee1Element As XElement = _
                                        <employee>
                                            <name><%= employee1.Name %></name>
                                        </employee>
            Console.WriteLine(employee1Element.ToString())
            '
            Console.ReadLine()
        End Sub
    End Class
    Class Employee
        Protected _Name As String
        Public Property Name() As String
            Get
                Name = _Name
            End Get
            Set(ByVal value As String)
                _Name = value
            End Set
        End Property
    End Class
End Namespace
Console Application Output
<employee>
  <name>John Smith</name>
</employee>
Of iets complexer, waar we ook de attribuut waarde uit het run time object halen :
Visual Basic 2010 Broncode
Namespace EmbeddedExpressions
    Class Example2
        Public Shared Sub Main()
            Dim e As New Employee With {.Name = "John Smith", _
                                       .Departement = New Departement _
                         With {.Area = "London", .Type = DepartementType.Sales}}
            '
            Dim employee1Element As XElement = _
                <employee>
                    <name><%= e.Name %></name>
                    <departement type=<%= e.Departement.Type %>
                        ><%= e.Departement.Area %></departement>
                </employee>
            Console.WriteLine(employee1Element.ToString())
            '
            Console.ReadLine()
        End Sub
    End Class
    Partial Class Employee
        Protected _Departement As Departement
        Public Property Departement() As Departement
            Get
                Departement = _Departement
            End Get
            Set(ByVal value As Departement)
                _Departement = value
            End Set
        End Property
    End Class
    Class Departement
        Protected _Area As String
        Public Property Area() As String
            Get
                Area = _Area
            End Get
            Set(ByVal value As String)
                _Area = value
            End Set
        End Property
        Protected _Type As DepartementType
        Public Property Type() As DepartementType
            Get
                Type = _Type
            End Get
            Set(ByVal value As DepartementType)
                _Type = value
            End Set
        End Property
    End Class
    Enum DepartementType
        Sales
        Distrubution
    End Enum
End Namespace
Console Application Output
<employee>
  <name>John Smith</name>
  <departement type="Sales">London</departement>
</employee>
Bemerk dat steeds de tekstuele representatie wordt gebruikt van de expressies in de embedded expressies.  Zo staat er bijvoorbeeld niet ... type="0" ..., maar type="Sales", ondanks de expressie e.Departement.Type evalueert naar 0'.

Bemerk ook hoe de omsluitende double quotes worden toegevoegd rond de attribuut waarde, ook iets wat we dus niet meer in onze XML literal hoeven op te nemen.

Zelfs de namen van bijvoorbeeld elementen of attributen kunnen we dynamisch ophalen :
Visual Basic 2010 Broncode
Namespace EmbeddedExpressions
    Class Example3
        Public Shared Sub Main()
            Dim elementName As String = "person"
            Dim attributeName As String = "name"
            '
            Dim personElement As XElement = _
                               <<%= elementName %> <%= attributeName %>="John"/>
            Console.WriteLine(personElement.ToString())
            '
            Console.ReadLine()
        End Sub
    End Class
End Namespace
Console Application Output
<person name="John" />

28.3.1. Embedded Expressions en Nothing

Let op een elementnaam ( XName van een XElement ) mag niet naar Nothing evalueren :
Visual Basic 2010 Broncode
Namespace EmbeddedExpressions
    Class Example4
        Public Shared Sub Main()
            Dim elementName As String '= Nothing
            '
            Try
                Dim element As XElement = <<%= elementName %>/>
            Catch ex As ArgumentNullException
                Console.WriteLine(ex.Message)
            End Try
            '
            Console.ReadLine()
        End Sub
    End Class
End Namespace
Console Application Output
Value cannot be null.
Parameter name: name
Bij de naam van een attribuut ( XName van een XAttribute ) mag dit wel, zolang de attribuutwaarde dan ook naar Nothing evalueert, anders krijgen we ook een ArgumentNullException :
Visual Basic 2010 Broncode
Namespace EmbeddedExpressions
    Class Example5
        Public Shared Sub Main()
            Dim attributeName As String '= Nothing
            Dim attributeValue As String '= Nothing
            '
            Dim element As XElement = _
                       <someElement <%= attributeName %>=<%= attributeValue %>/>
            Console.WriteLine(element.ToString()) ' <someElement />
            '
            attributeValue = "someAttributeValue"
            Try
                element = <someElement <%= attributeName %>=<%= attributeValue %>/>
            Catch ex As ArgumentNullException
                Console.WriteLine(ex.Message)
            End Try
            '
            attributeName = "someAttributeName"
            attributeValue = Nothing
            element = <someElement <%= attributeName %>=<%= attributeValue %>/>
            Console.WriteLine(element.ToString()) ' <someElement />
            '
            Console.ReadLine()
        End Sub
    End Class
End Namespace
Console Application Output
<someElement />
Value cannot be null.
Parameter name: name
<someElement />
De attribuutwaarde mag altijd Nothing zijn.

28.3.2. XElement of XAttribute als Embedded Expressies

Ook XElement of XAttribute objecten bijvoorbeeld kan men als embedded expressies gebruiken.
Hieronder een voorbeeld met een XAttribute object :
Visual Basic 2010 Broncode
Namespace EmbeddedExpressions
    Class Example6
        Public Shared Sub Main()
            Dim e As New Employee With {.Name = "John Smith", _
                           .Departement = New Departement _
             With {.Area = "London", .Type = departementType.Sales}}
            '
            Dim attributeName As XName = XName.Get(e.Departement.Type.ToString())
            Dim attributeValue As String = e.Departement.Area
            '
            Dim attribute As New XAttribute(attributeName, attributeValue)
            Console.WriteLine(attribute.ToString()) ' Sales="London"
            '
            Dim element As XElement = _
                             <employee <%= attribute %>><%= e.Name %></employee>
            Console.WriteLine(element.ToString())
            '
            Console.ReadLine()
        End Sub
    End Class
End Namespace
Console Application Output
Sales="London"
<employee Sales="London">John Smith</employee>
Een voorbeeld met een XElement object :
Visual Basic 2010 Broncode
Namespace EmbeddedExpressions
    Class Example7
        Public Shared Sub Main()
            Dim element As XElement = <someElement/>
            Dim document As XDocument = <?xml version="1.0"?><%= element %>
            Console.WriteLine(document.ToString())
            '
            Console.ReadLine()
        End Sub
    End Class
End Namespace
Console Application Output
<someElement />
Ook collecties van XElement of XAttribute objecten kan je als embedded expressies gebruiken.

In onderstaand voorbeeld bevat de array children meerdere XElement objecten die worden ingevoegd in <node1> :
Visual Basic 2010 Broncode
Namespace EmbeddedExpressions
    Class Example8
        Public Shared Sub Main()
            Dim children As XElement() = New XElement() {<node2/>, <node3/>}
            Dim element As XElement = <node1><%= children %></node1>
            Console.WriteLine(element.ToString())
            '
            Console.ReadLine()
        End Sub
    End Class
End Namespace
Console Application Output
<node1>
  <node2 />
  <node3 />
</node1>
Als een XElement of XAttribute object ( of collecties hiervan ) als embedded expressie naar Nothing evalueert wordt dit door de compiler genegeerd.  Hierbij worden dus geen excepties opgeworpen.

Dit artikel is gepubliceerd op zondag 31 juli 2011 op vbvoorbeelden, bezoek de website voor een recente versie van dit artikel of andere artikels.