How to insert XElements into XML literals in VB.NET?

In VB.NET, I can easily insert strings into XML literals with <xml><%= "my string" %></xml>.

How to insert an instance XElement?

I know that I can use methods in the XElement, XNode, etc. classes, but I would like to do this in XML literals, if possible.

+5
source share
2 answers

Looks like I can just do the following:

Function GetSomeMoreXml() As XElement
   Return <moreXml/>
End Function

Sub Main()
   Dim myXml = <myXml>
                  <%= GetSomeMoreXml() %>
               </myXml>
End Sub

This is pretty neat. This allows me to break my XML literals into more manageable pieces.

+7
source

If you really need to do this, you can always do this:

<xml><%= myXElement.ToString() %></xml>

, . , ? XElement, , ( , ).

-1

All Articles