有没有办法确定 XElement 是否包含任何指定元素之一?例如,我有要检查的 XElements:
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>
我希望能够查询任何 xelements 以查看它们是否包含元素或在它们之下,如果是则返回 true,否则返回 false <red/>
。<green/>
<blue/>
我希望它会更简单,但我能想到的最好的是:
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(list of elements) 之类的东西?