C# – How to determine if XElement.Elements() contains a node with a specific name

.net-3.5clinqxml

For example for the following XML

 <Order>
  <Phone>1254</Phone>
  <City>City1</City>
  <State>State</State>
 </Order>

I might want to find out whether the XElement contains "City" Node or not.

Best Answer

Just use the other overload for Elements.

bool hasCity = OrderXml.Elements("City").Any();