Checking an XElement for Existence of One of Several Possible XElements

喜欢而已 提交于 2019-12-06 15:44:59

If I understand correctly - perhaps (using C#, sorry - but no real C# specific logic here):

var colors = new[] {"red", "green","blue"};
bool any = el.Descendants().Any(child => colors.Contains(child.Name.LocalName));

Even if the VB fights you, I'm sure you can use .Any instead of .SingleOrDefault and a null 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:

<somexml><color>blue</color></somexml>

or even as an attribute:

<somexml color="blue"/>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!