public HtmlNode AppendChild(string type) { var newElement = _htmlNode.OwnerDocument.CreateElement(type); _htmlNode.AppendChild(newElement); return(new HtmlNode(newElement)); }
internal void Write(HtmlAgilityPack.HtmlNode parent) { var questionNode = parent.OwnerDocument.CreateElement("div"); questionNode.Attributes.Add("class", "question"); questionNode.InnerHtml = question; parent.AppendChild(questionNode); foreach (var answer in answers) { var answerNode = parent.OwnerDocument.CreateElement("div"); answerNode.Attributes.Add("class", "answer"); answerNode.InnerHtml = answer; parent.AppendChild(answerNode); } }
public HtmlAgilityPack.HtmlDocument Write(HtmlAgilityPack.HtmlNode parent) { foreach (var task in tasks) { HtmlAgilityPack.HtmlNode child = doc.CreateElement("div"); child.Attributes.Add("class", "task"); task.Write(child); parent.AppendChild(child); } return(doc); }
public string monPage() { Uri uri = new Uri(DBsettings.DBURL + "mon.php"); string resp = null; //monster list doc HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument(); //monster detail doc //HtmlAgilityPack.HtmlDocument subdoc = new HtmlAgilityPack.HtmlDocument(); //tempary doc HtmlAgilityPack.HtmlDocument _doc = new HtmlAgilityPack.HtmlDocument(); //create empty table monTable = null; outHtml = ""; monUrlSet = new HashSet <string>(); monTable = _doc.CreateElement("table"); //monTable.AddClass("table table-striped table-bordered"); monTable.AppendChild(_doc.CreateElement("thead")); monTable.AppendChild(_doc.CreateElement("tbody")); var theadNode = monTable.SelectSingleNode(".//thead"); var monList = new HtmlAgilityPack.HtmlNode[] { }; var simpleCrawlResult = WebRequest.RequestAction(new RequestOptions() { Uri = uri, Method = "Get" }); //load monster list doc.LoadHtml(simpleCrawlResult); var monSpan = doc.DocumentNode.SelectSingleNode("//h5[@class='card-header']").InnerHtml; resp = monSpan; areaTab_Main = doc.DocumentNode.SelectSingleNode("//div[@id = 'Monstermon_list15']"); string tableClass = areaTab_Main.SelectSingleNode(".//table").GetAttributeValue("class", ""); monTable.AddClass(tableClass); HtmlAgilityPack.HtmlNode[] mons = areaTab_Main.SelectNodes(".//tbody/tr[position() < 5]").ToArray(); Console.Out.WriteLine("Total " + mons.Count() + " monster fetched"); //var _thead = monTable.SelectSingleNode(".//thead"); //add thead if (_thead == "") { string[] monTh = DBsettings.monTableHead; foreach (var th in monTh) { _thead += "<th>" + th + "</th>"; } theadNode.InnerHtml = _thead; } if (theadNode.InnerHtml == "") { theadNode.InnerHtml = _thead; } foreach (var mon in mons) { outHtml += monDetailPg(mon); } monTable.SelectSingleNode(".//tbody").InnerHtml += outHtml; resp += monTable.OuterHtml; return(resp); }
/// <summary> /// Prepare Html /// </summary> /// <param name="input"></param> /// <param name="output"></param> private void PrepareHtml(HtmlAgilityPack.HtmlNode input, HtmlAgilityPack.HtmlNode output) { HtmlAgilityPack.HtmlNode parent = output; switch (input.NodeType) { case HtmlAgilityPack.HtmlNodeType.Document: break; case HtmlAgilityPack.HtmlNodeType.Element: if (!availableTags.Contains(input.OriginalName)) { return; } if (input.OriginalName.Equals("img")) { output.AppendChild(input.CloneNode(true)); return; } parent = output.AppendChild(input.CloneNode(false)); string newStyle = string.Empty; string style = input.GetAttributeValue("style", string.Empty); string href = input.GetAttributeValue("href", string.Empty); parent.Attributes.RemoveAll(); if (style != string.Empty) { foreach (string item in style.Split(';')) { if ((item.Contains("font-weight") && item.Contains("bold")) || (item.Contains("font-style") && item.Contains("italic")) || (item.Contains("text-decoration") && item.Contains("underline"))) { newStyle += string.Format("{0};", item); } } } if (newStyle != string.Empty) { parent.SetAttributeValue("style", newStyle); } if (href != string.Empty) { parent.SetAttributeValue("href", href); } break; case HtmlAgilityPack.HtmlNodeType.Text: output.AppendChild(input.CloneNode(true)); return; default: return; } foreach (HtmlAgilityPack.HtmlNode child in input.ChildNodes) { PrepareHtml(child, parent); } }