检查 XElement 是否存在多个可能的 XElement 之一
有没有办法确定 XElement 是否包含任何指定元素之一?例如,我有要检查的 XElement:
Dim xe1 = <color><blue/></color>
Dim xe2 = <color><red/></color>
Dim xe3 = <color><powderBlue/></color>
Dim xe4 = <color><aqua/></color>
Dim xe5 = <color><green/></color>
我希望能够查询任何 XElement 以查看它们是否包含元素
、
或
在它们下面,如果是则返回 true,否则返回 false。
我希望它会更简单,但我能想到的最好方法是:
Dim primaryColor = From e In xe1.Elements Where e.Name = "blue" Or e.Name = "red" Or e.Name = "green"
Dim primaryColorTrue = primaryColor.SingleorDefault
If primaryColorTrue IsNot Nothing Then
'Blah
End If
有没有人有更好的方法来做到这一点,例如将红色/绿色/蓝色的 xelements 放入数组中并使用 Elements.Contains 之类的东西(元素列表)?
Is there a way to determine if an XElement contains one of any specified elements? For example, I have XElements that I'll want to check:
Dim xe1 = <color><blue/></color>
Dim xe2 = <color><red/></color>
Dim xe3 = <color><powderBlue/></color>
Dim xe4 = <color><aqua/></color>
Dim xe5 = <color><green/></color>
I'd like to be able to query any of the xelements to see if they containt the elements <red/>
, <green/>
or <blue/>
below them and return true if so, false if not.
I was hoping that it would be simplier, but the best I could come up with was:
Dim primaryColor = From e In xe1.Elements Where e.Name = "blue" Or e.Name = "red" Or e.Name = "green"
Dim primaryColorTrue = primaryColor.SingleorDefault
If primaryColorTrue IsNot Nothing Then
'Blah
End If
Does anyone have a better way to do this, such as putting those xelements of red/green/blue into an array and using something like Elements.Contains(list of elements)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果我理解正确的话 - 也许(使用 C#,抱歉 - 但这里没有真正的 C# 特定逻辑):
即使 VB 与你战斗,我相信你可以使用
.Any
而不是。 SingleOrDefault
和null
检查。对于信息来说,对我来说使用 elements 听起来像是一个奇怪的想法;如果可能的话,我只需将颜色名称作为文本:
或者甚至作为属性:
If I understand correctly - perhaps (using C#, sorry - but no real C# specific logic here):
Even if the VB fights you, I'm sure you can use
.Any
instead of.SingleOrDefault
and anull
check.For info, using elements here to me sounds like an odd idea; I'd just have the color name as the text if possible:
or even as an attribute: