public List <string> ParseList(XpathExtractModel model) { if (model == null || string.IsNullOrEmpty(model.XpathRule)) { return(null); } HtmlNodeCollection nodes = this.htmlNode?.SelectNodes(model.XpathRule); if (nodes == null || nodes.Count <= 0) { return(null); } if (model.XpathEndAttributes != null && model.XpathEndAttributes.Count > 0) { return(nodes.Select(n => n.Attributes.Where(a => model.XpathEndAttributes.Contains(a.Name)).Select(a => a.Value?.Trim()).FirstOrDefault()). Where(n => !string.IsNullOrEmpty(n)) .ToList()); } switch (model.ExtractType) { case ExtractType.Text: return(nodes.Select(n => n.InnerText.Trim()).Where(n => !string.IsNullOrEmpty(n)).ToList()); case ExtractType.Html: default: return(nodes.Select(n => n.InnerHtml.Trim()).Where(n => !string.IsNullOrEmpty(n)).ToList()); } }
public string ParseFirst(XpathExtractModel model) { List <string> results = this.ParseList(model); if (results == null || results.Count <= 0) { return(null); } return(results.FirstOrDefault()); //if (this._rootNode == null || model == null || string.IsNullOrEmpty(model.XpathRule)) return null; //HtmlNodeCollection nodes = this._rootNode.SelectNodes(model.XpathRule); //if (nodes == null) return null; //switch (model.ExtractType) //{ // case ExtractType.Text: // return nodes.FirstOrDefault().InnerText.Trim(); // case ExtractType.Html: // default: // return nodes.FirstOrDefault().InnerHtml.Trim(); //} }
public string ParseFirst(XpathExtractModel model) { List <string> results = this.ParseList(model); if (results == null || results.Count <= 0) { return(null); } return(results.FirstOrDefault()); }
public List <string> ParseList(XpathExtractModel model) { if (this._rootNode == null || model == null || string.IsNullOrEmpty(model.XpathRule)) { return(null); } HtmlNodeCollection nodes = this._rootNode.SelectNodes(model.XpathRule); return(this.GetNodeValues(nodes, model.XpathEndAttributes, model.ExtractType)); }