Is it possible to make a node optional in VB.NET XML Literals?

272 views Asked by At
Dim task As XElement = <task>
                               <body>body</body>
                               <optional><%= myVar %></optional>

                           </task>

Is there anyway to remove <optional> when myVar Is Nothing?

1

There are 1 answers

0
msmucker0527 On

You can use an IIf

    Dim task As XElement = <task>
                               <body>body</body>
                               <%= IIf(String.IsNullOrEmpty(myVar), "", <optional><%= myVar %></optional>) %>
                           </task>