FindParent(string parentName) - For XElement

        public static XElement FindParent(this XElement e, string Name)
        {
            XElement r = null;

            if (e == null)
                return r;
            
            if (e.Parent != null && e.Parent.Name == Name)
            {
                r = e.Parent;
            }
            else
            {
                r = e.Parent.FindParent(Name);
            }

            return r;
        }
Example:
XElement parentXElement = childXElement.FindParent("nodename");

Description

Find parent XElement from a provided name. Returns null if no match

Details

Double click on the code to select all.

 

;