VB.Net、Linq to Xml:这是创建 xml 时使用逻辑的正确方法吗?

发布于 2024-10-19 13:34:31 字数 671 浏览 3 评论 0原文

在构建 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

乱了心跳 2024-10-26 13:34:31

当您在一行中使用 If 子句时,您有 2 个重载:

 IF(condition, true, false) 

或者

 If(Condition,False)

您可以编写类似这样的内容以避免不分配任何值:

If(buildElement is nothing,<BuildMyElement><%= buildElement.ToString %></BuildMyElement>)

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>)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文