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".
string xmlData = @"<ProjectedIncomePosition><Coupon>0.59</Coupon><IsExternal>false</IsExternal><MaturityDate/></ProjectedIncomePosition>";
// Getting all elements.
IEnumerable<XElement> roots = XElement.Parse(xmlData).Elements();
// Getting coupon node index
int coupon = roots.Select((item, index) => new { elementName = item.Name, Index = index }).Where(p => p.elementName == "Coupon").Select(p => p.Index + 1).FirstOrDefault();
// Getting maturity node index
int maturityDate = roots.Select((item, index) => new { elementName = item.Name, Index = index }).Where(p => p.elementName == "MaturityDate").Select(p => p.Index + 1).FirstOrDefault();
if (coupon < maturityDate)
{
// Correct
}
else
{
// 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.
