条件 XML 文字

发布于 2024-08-11 05:28:33 字数 943 浏览 6 评论 0原文

我尝试过搜索此问题,但似乎无法在任何地方找到答案,所以希望这里有人可以提供帮助。我想根据计数是否大于 1 插入条件 XML 标记,但不知道该怎么做。

例如,我有两个像这样的 XElement:

<Blob>
<Group>
Stuff 1
</Group>
</Blob>

<Blob>
<Group>
Stuff 1
</Group>
<Group>
Stuff 2
</Group>
</Blob>

希望这最终适用于第一个:

<BigGroup>
<Group/>
</BigGroup>

和第二个:

<BigGroup>
<Groups>
<Group/>
<Group/>
</Groups>
</BigGroup>

请注意,在第二个中,它们是包裹在两个组周围的标签。

所以,我想要 XML 文字中的条件。我尝试过的是:

Dim groups = If(<Blob>.<Group>.Count > 1, <Groups/>, Nothing)

Dim bigGroup = <BigGroup><%= groups %><%= from e in <Blob>.<Group> select e %><%= groups%></BigGroup>

但这不起作用。有没有人可以按照上面的要求从 XML Literal 中执行此操作?

I've tried searching on this but can't seem to find an answer anywhere, so hopefully someone here can help. I want to insert an conditional XML tag based on whether or not a count is above one, but am not sure how to do it.

For example, I have two XElements that are like this:

<Blob>
<Group>
Stuff 1
</Group>
</Blob>

and

<Blob>
<Group>
Stuff 1
</Group>
<Group>
Stuff 2
</Group>
</Blob>

I want this to end up being for the first one:

<BigGroup>
<Group/>
</BigGroup>

and for the second one:

<BigGroup>
<Groups>
<Group/>
<Group/>
</Groups>
</BigGroup>

Notice in the second one, their is a tag of wrapped around the two groups.

So, I want that condition in the XML Literal. What I've tried is:

Dim groups = If(<Blob>.<Group>.Count > 1, <Groups/>, Nothing)

Dim bigGroup = <BigGroup><%= groups %><%= from e in <Blob>.<Group> select e %><%= groups%></BigGroup>

But that is not working. Does anyone have a way to do this from within the XML Literal as desired above?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

苏辞 2024-08-18 05:28:33

您只需将 If 语句放在 XML Literal 中即可。此外,它足够智能,只需引用 <%= %> 内的变量即可输出 IEnumerable(Of XElement)

这是代码。

Sub Main()
    Dim SingleGroup = <Blob>
                          <Group>Stuff 1</Group>
                      </Blob>

    Dim TwoGroups = <Blob>
                        <Group>Stuff 1</Group>
                        <Group>Stuff 2</Group>
                    </Blob>

    Dim BigGroup1 = BigGroup(SingleGroup.<Group>)

    Dim BigGroup2 = BigGroup(TwoGroups.<Group>)

    Console.WriteLine(BigGroup1)
    Console.WriteLine()

    Console.WriteLine(BigGroup2)
    Console.ReadLine()

End Sub

Function BigGroup(ByVal groups As IEnumerable(Of XElement)) As XElement
    Dim result = <BigGroup>
                     <%= If(groups.Count > 1, _
                         <Groups><%= groups %></Groups>, _
                         groups.SingleOrDefault) %>
                 </BigGroup>
    Return result
End Function

输出是:

<BigGroup>
  <Group>Stuff 1</Group>
<BigGroup>

<BigGroup>
  <Groups>
    <Group>Stuff 1</Group>
    <Group>Stuff 2</Group>
  </Groups>
</BigGroup>

You can just place the If statement inside the XML Literal. Also, it's smart enough to output an IEnumerable(Of XElement) by simply referencing the variable inside <%= %>.

Here's the code.

Sub Main()
    Dim SingleGroup = <Blob>
                          <Group>Stuff 1</Group>
                      </Blob>

    Dim TwoGroups = <Blob>
                        <Group>Stuff 1</Group>
                        <Group>Stuff 2</Group>
                    </Blob>

    Dim BigGroup1 = BigGroup(SingleGroup.<Group>)

    Dim BigGroup2 = BigGroup(TwoGroups.<Group>)

    Console.WriteLine(BigGroup1)
    Console.WriteLine()

    Console.WriteLine(BigGroup2)
    Console.ReadLine()

End Sub

Function BigGroup(ByVal groups As IEnumerable(Of XElement)) As XElement
    Dim result = <BigGroup>
                     <%= If(groups.Count > 1, _
                         <Groups><%= groups %></Groups>, _
                         groups.SingleOrDefault) %>
                 </BigGroup>
    Return result
End Function

The output is:

<BigGroup>
  <Group>Stuff 1</Group>
<BigGroup>

<BigGroup>
  <Groups>
    <Group>Stuff 1</Group>
    <Group>Stuff 2</Group>
  </Groups>
</BigGroup>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文