VB.Net,Linq到Xml:这是创建xml时使用逻辑的正确方法吗? [英] VB.Net, Linq to Xml: Is this the correct way to use logic while creating xml?
本文介绍了VB.Net,Linq到Xml:这是创建xml时使用逻辑的正确方法吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在构建xml文档时,我需要使用逻辑来指示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>
推荐答案
在一行中使用If子句时,有2个重载:
when you use the If clause in one line you have 2 overload that are:
IF(condition, true, false)
或
If(Condition,False)
您可以编写类似这样的内容,以避免不分配任何值:
You could write something like this to avoid assigning nothing value:
If(buildElement is nothing,<BuildMyElement><%= buildElement.ToString %></BuildMyElement>)
这篇关于VB.Net,Linq到Xml:这是创建xml时使用逻辑的正确方法吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文