开发者

How can I embed XElements into XML literals in VB.NET?

开发者 https://www.devze.com 2023-01-09 02:18 出处:网络
In VB.NET I can easily embed strings into XML literals using <xml><%= \"my string\" %></xml>.

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 开发者_如何学Pythoncan use methods on the XElement, XNode, etc classes, but I'd like to do it in the XML literals if possible.


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.


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).

0

精彩评论

暂无评论...
验证码 换一张
取 消