条件 XML 文字
我尝试过搜索此问题,但似乎无法在任何地方找到答案,所以希望这里有人可以提供帮助。我想根据计数是否大于 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您只需将 If 语句放在 XML Literal 中即可。此外,它足够智能,只需引用
<%= %>
内的变量即可输出IEnumerable(Of XElement)
。这是代码。
输出是:
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.
The output is: