While building an xml document I require to use logic to dictate the outcome of the xml; logically it is similar to the following piece of code (although this does not work):
Dim buildElement As Boolean = True
Dim xe As XElement = _
<xml>
<% If buildElement Then %>
<BuildMyElement><%= buildElement.ToString %></BuildMyElement>
<% End If %>
</xml>
I have managed to do this using the method show below, is this开发者_如何学编程 the suggested way of doing this or is there a better one??
Dim buildElement As Boolean = True
Dim xe As XElement = _
<xml>
<%= If(buildElement, _
<BuildMyElement><%= buildElement.ToString %></BuildMyElement>, _
Nothing) %>
</xml>
when you use the If clause in one line you have 2 overload that are:
IF(condition, true, false)
or
If(Condition,False)
You could write something like this to avoid assigning nothing value:
If(buildElement is nothing,<BuildMyElement><%= buildElement.ToString %></BuildMyElement>)
精彩评论