BExIS.Xml.Helpers.XExtentsions.IndexPosition C# (CSharp) Метод

IndexPosition() публичный статический Метод

Get the index of the given XElement relative to its siblings with identical names. If the given element is the root, -1 is returned.
public static IndexPosition ( this element ) : int
element this /// The element to get the index of. ///
Результат int
        public static int IndexPosition(this XElement element)
        {
            if (element == null)
            {
                throw new ArgumentNullException("element");
            }

            if (element.Parent == null)
            {
                return -1;
            }

            int i = 1; // Indexes for nodes start at 1, not 0

            foreach (var sibling in element.Parent.Elements(element.Name))
            {
                if (sibling == element)
                {
                    return i;
                }

                i++;
            }

            throw new InvalidOperationException
                ("element has been removed from its parent.");
        }