Recursion with XML Literals in VB.NET is possible?

独自空忆成欢 提交于 2019-12-05 05:09:18

Recursion is one of the reasons I love VB.NET XML Literals!

In order to do the recursion, you need a function that accepts a ProfileItems collection and returns an XElement. Then you can recursively call that function inside your XML Literal.

Also, in order for the recursion to work, GetProfileItems and GetDependencies need to have the same name (rename one of them) and display with the same Xml Element structure. Here's what the recursive function might look like:

Function GetProfileItemsElement(ByVal Items As List(Of ProfileItem) As XElement
    Return <items>
               <%= From i In Items _
                   Select <item>
                              <name><%= i.Name %></name>
                              <!-- other elements here -->
                              <%= GetProfileItemsElement(i.GetDependencies) %>
                          </item> %>
           </items>
End Function

The recursion will end when you get to an item that returns an empty list for the GetDependencies function. In that case, the nested items element will be empty: <items/>. XML Literals are smart enough to combine the start and end items tags when there aren't any child elements.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!