You can use the NodesBeforeSelf method for this:
XElement root = new XElement("root", new XElement("one", new XElement("oneA"), new XElement("oneB") ), new XElement("two"), new XElement("three") ); foreach (XElement x in root.Elements()) { Console.WriteLine(x.Name); Console.WriteLine(x.NodesBeforeSelf().Count()); }
Update. If you really need the Position method, just add an extension method.
public static class ExMethods { public static int Position(this XNode node) { return node.NodesBeforeSelf().Count(); } }
Now you can just call x.Position (). :)
Bryant
source share