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

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

public static GetAbsoluteXPathWithIndex ( this element ) : string
element this
Результат string
        public static string GetAbsoluteXPathWithIndex(this XElement element)
        {
            if (element == null)
            {
                throw new ArgumentNullException("element");
            }

            Func<XElement, string> relativeXPath = e =>
            {
                int index = e.IndexPosition();

                if (index == -1) index = 1;

                var currentNamespace = e.Name.Namespace;

                string name = "";
                if (currentNamespace == null)
                {
                    name = e.Name.LocalName;
                }
                else
                {
                    //string namespacePrefix = e.GetPrefixOfNamespace(currentNamespace);
                    name += "/" + e.Name.LocalName +"["+index+"]";
                }

                return name;
                // If the element is the root, no index is required
                //return (index == -1) ? "/" + name : string.Format
                //(
                //    "/{0}[{1}]",
                //    name,
                //    index.ToString()
                //);
            };

            var ancestors = from e in element.Ancestors()
                            select relativeXPath(e);

            return string.Concat(ancestors.Reverse().ToArray()) +
                   relativeXPath(element);
        }