递归与XML文本在VB.NET是可能的吗? [英] Recursion with XML Literals in VB.NET is possible?

查看:119
本文介绍了递归与XML文本在VB.NET是可能的吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一类叫做个人资料,有一些简单的属性,然后它可以有集合 ProfileItem 一遍有一些简单的属性,然后就可以有一个集合对 ProfileItem (递归)。

I have a class called Profile that has some simple properties and then it can have a collection of ProfileItem that again has some simple properties and then it can have a collection of ProfileItem (RECURSION).

现在我使用的XML文本来与VB.NET(3.5)试图产生一个非常简单的保存功能。

Now I am trying to generated a very simple save function using XML Literals that come with VB.NET (3.5).

在code我现在用的是以下内容:

The code I am using is the following:

  Dim xdoc As XDocument = _
            <?xml version="1.0" encoding="utf-8"?>
            <profiles>
                <%= _
                    From p In _Profiles _
                    Select <profile name=<%= p.Name %>>
                               <%= _
                                   From i In p.GetProfileItems _
                                   Select <item>
                                              <name><%= i.Name %></name>
                                              <action><%= i.Action.ToString %></action>
                                              <type><%= i.Type.ToString %></type>
                                              <arguments><%= i.Arguments %></arguments>
                                              <dependencies>
                                                  <%= _
                                                      From d In i.GetDependencies _
                                                      Select <dependency>
                                                                 <name><%= d.Name %></name>
                                                             </dependency> _
                                                  %>
                                              </dependencies>
                                          </item> _
                               %>
                           </profile> _
                %>
            </profiles>

相关标记的部分应成为递归的,但我不知道它是否以某种方式通过这个语法的支持。

The part related to tag should become recursive, but I don't know if it is in some way supported by this syntax.

我应该重写XML文本的所有避免使用,以实现递归?

Should I rewrite all avoiding usage of XML Literal to implement recursion?

推荐答案

递归是我喜欢VB.NET XML文本的原因之一!

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

为了做递归,你需要接受一个ProfileItems收集和返回的XElement的功能。然后,您可以递归调用该函数的XML文本内。

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.

另外,为了递归工作,GetProfileItems和GetDependencies需要有相同的名称(重命名其中之一),并显示与相同的XML元素的结构。这里的递归函数可能看起来是这样的:

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

当你得到一个返回一个空的列表中GetDependencies功能的项目的递归将结束。在这种情况下,嵌套的项目元素将是空的:&LT;项目/&GT; 。 XML文本有足够的智慧结合起来,开始和结束项目标签时,不会有任何的子元素。

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.

这篇关于递归与XML文本在VB.NET是可能的吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆