Html Agility Pack – Problem selecting subnode

A few things that will help you when working with HtmlAgilityPack and XPath expressions. If run is an HtmlNode, then: run.SelectNodes(“//div[@class=”date”]”) Will will behave exactly like doc.DocumentNode.SelectNodes(“//div[@class=”date”]”) run.SelectNodes(“./div[@class=”date”]”) Will give you all the <div> nodes that are children of run node. It won’t search deeper, only at the very next depth level. run.SelectNodes(“.//div[@class=”date”]”) Will return … Read more

HtmlAgilityPack replace node

To create a new node, use the HtmlNode.CreateNode() factory method, do not use the constructor directly. This code should work out for you: var htmlStr = “<b>bold_one</b><strong>strong</strong><b>bold_two</b>”; var doc = new HtmlDocument(); doc.LoadHtml(htmlStr); var query = doc.DocumentNode.Descendants(“b”); foreach (var item in query.ToList()) { var newNodeStr = “<foo>bar</foo>”; var newNode = HtmlNode.CreateNode(newNodeStr); item.ParentNode.ReplaceChild(newNode, item); } Note … Read more

HtmlAgilityPack set node InnerText

Try code below. It select all nodes without children and filtered out script nodes. Maybe you need to add some additional filtering. In addition to your XPath expression this one also looking for leaf nodes and filter out text content of <script> tags. var nodes = doc.DocumentNode.SelectNodes(“//body//text()[(normalize-space(.) != ”) and not(parent::script) and not(*)]”); foreach (HtmlNode … Read more

XPath wildcard in attribute value

Use the following expression: //span[contains(concat(‘ ‘, @class, ‘ ‘), ‘ amount ‘)] You could use contains on its own, but that would also match classes like someamount. Test the above expression on the following input: <root> <span class=”test amount blah”/> <span class=”amount test”/> <span class=”test amount”/> <span class=”amount”/> <span class=”someamount”/> </root> It will select the … Read more

Parsing HTML page with HtmlAgilityPack

There are a number of ways to select elements using the agility pack. Let’s assume we have defined our HtmlDocument as follows: string html = @”<TD class=texte width=””50%””> <DIV align=right>Name :<B> </B></DIV></TD> <TD width=””50%””> <INPUT class=box value=John maxLength=16 size=16 name=user_name> </TD> <TR vAlign=center>”; HtmlDocument htmlDoc = new HtmlDocument(); htmlDoc.LoadHtml(html); 1. Simple LINQ We could use … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)