开发者

VB.Net, Linq to Xml: Is this the correct way to use logic while creating xml?

开发者 https://www.devze.com 2023-02-14 01:40 出处:网络
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):

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>)
0

精彩评论

暂无评论...
验证码 换一张
取 消