如何检查xElement值是否存在?

时间:2016-12-14 15:26:18

标签: c# xml linq xelement

我需要检查一下“Charrizard'存在,我正在环顾网络,但只找到了xElement属性值和子节点示例。

<pokemons>
  <pokemon>
    <color>red</color>
    <name>Charrizard</name> //the content is named value right ??
  </pokemon>
</pokemons>

我看到它的起点就像:

XDocument doc = XDocument.Load("pokemons.xml");
bool b =  doc.Descendants(but don't know how to access the value.)..

1 个答案:

答案 0 :(得分:2)

bool b =  doc.Descendants("name").Any(x=> x.Value == "Charrizard");

您可以使用Enumerable.Any

实现这一目标
  

确定序列的任何元素是否满足条件。

dotNetFiddle

中的完整示例
相关问题