How to check if xElement value exists?

2.3k views Asked by At

I need to check for example if 'Charrizard' exists, I was looking around the web but only found xElement Attribute value and child nodes examples.

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

I saw somewhere it starts like:

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

There are 1 answers

3
mybirthname On BEST ANSWER
bool b =  doc.Descendants("name").Any(x=> x.Value == "Charrizard");

You achieve that using Enumerable.Any

Determines whether any element of a sequence satisfies a condition.

Here full example in dotNetFiddle