/// <summary> /// 获取下级元素 /// </summary> /// <param name="elementName"></param> /// <returns></returns> public static List <HtmlInfo> ChildDescendants(this IEnumerable <HtmlInfo> htmlInfoList, string elementName = null) { var html = htmlInfoList.First().InnerHtml; XHtmlElement xhe = new XHtmlElement(html); return(xhe.RootDescendants(html).Where(c => elementName == null || c.TagName == elementName).ToList()); }
/// <summary> /// 获取父级 /// </summary> /// <param name="htmlInfoList"></param> /// <returns></returns> public static List <HtmlInfo> ParentDescendant(this IEnumerable <HtmlInfo> htmlInfoList, string fullHtml) { var saveLeveHtml = htmlInfoList.First().SameLeveHtml; string replaceGuid = Guid.NewGuid().ToString(); fullHtml = fullHtml.Replace(saveLeveHtml, replaceGuid); var parentHtml = Regex.Match(fullHtml, @"<[^<]+?>[^<]*?" + replaceGuid + @".*?<\/.+?>").Value; parentHtml = parentHtml.Replace(replaceGuid, saveLeveHtml); XHtmlElement xhe = new XHtmlElement(parentHtml); return(xhe.RootDescendants()); }