开发者

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

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜