VB.Net、Linq to Xml:这是创建 xml 时使用逻辑的正确方法吗?
在构建 xml 文档时,我需要使用逻辑来指示 xml 的结果;从逻辑上讲,它类似于下面的代码(尽管这不起作用):
Dim buildElement As Boolean = True
Dim xe As XElement = _
<xml>
<% If buildElement Then %>
<BuildMyElement><%= buildElement.ToString %></BuildMyElement>
<% End If %>
</xml>
我已经设法使用下面显示的方法来做到这一点,这是建议的方法还是有更好的方法?
Dim buildElement As Boolean = True
Dim xe As XElement = _
<xml>
<%= If(buildElement, _
<BuildMyElement><%= buildElement.ToString %></BuildMyElement>, _
Nothing) %>
</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>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当您在一行中使用 If 子句时,您有 2 个重载:
或者
您可以编写类似这样的内容以避免不分配任何值:
when you use the If clause in one line you have 2 overload that are:
or
You could write something like this to avoid assigning nothing value: