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