How can I embed XElements into XML literals in VB.NET?
-
29-09-2020 - |
Question
In VB.NET I can easily embed strings into XML literals using <xml><%= "my string" %></xml>
.
How can I embed an XElement
instance?
I know I can use methods on the XElement, XNode, etc classes, but I'd like to do it in the XML literals if possible.
Solution
It turns I can simply do the following:
Function GetSomeMoreXml() As XElement
Return <moreXml/>
End Function
Sub Main()
Dim myXml = <myXml>
<%= GetSomeMoreXml() %>
</myXml>
End Sub
Which is pretty neat. It allows me to break up my XML literals into more manageable chunks.
OTHER TIPS
If you really need to do that, you could always just do this:
<xml><%= myXElement.ToString() %></xml>
I can't think of any example where you would want to do this though. Care to elaborate on why you need this? It would have to write out the XElement string, then parse it before adding it back into the object model (I imagine that's how it would have to work at least).
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow