Find Index of a Node in XML using LINQ

Below code snippet defines to find index of a node. Suppose there is a requirement: two nodes "Coupon" and "MaturityDate" are there. Functionality is "Coupon" tag should come before "MaturityDate". 
  1. string xmlData = @"<ProjectedIncomePosition><Coupon>0.59</Coupon><IsExternal>false</IsExternal><MaturityDate/></ProjectedIncomePosition>";  
  2.   
  3. // Getting all elements.  
  4. IEnumerable<XElement> roots = XElement.Parse(xmlData).Elements();  
  5.   
  6. // Getting coupon node index  
  7. int coupon = roots.Select((item, index) => new { elementName = item.Name, Index = index }).Where(p => p.elementName == "Coupon").Select(p => p.Index + 1).FirstOrDefault();  
  8.   
  9. // Getting maturity node index  
  10. int maturityDate = roots.Select((item, index) => new { elementName = item.Name, Index = index }).Where(p => p.elementName == "MaturityDate").Select(p => p.Index + 1).FirstOrDefault();  
  11.   
  12. if (coupon < maturityDate)  
  13. {  
  14.     // Correct  
  15. }  
  16. else  
  17. {  
  18.     // Incorrect data  
Additionally you can use above code snippet to check whether a node is exist or not. If you are searching a node and it returns "0" then it doesn't exist.
 
Hope this helps you.