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>";
-
-
- IEnumerable<XElement> roots = XElement.Parse(xmlData).Elements();
-
-
- int coupon = roots.Select((item, index) => new { elementName = item.Name, Index = index }).Where(p => p.elementName == "Coupon").Select(p => p.Index + 1).FirstOrDefault();
-
-
- 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)
- {
-
- }
- else
- {
-
- }
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.