How to get text between two div tags with some class attribute with HTMLagility

c# dom html html-agility-pack selector

Question

I want to get some text from two html div from HTML file. After some searches i decided to use HTMLAgility Pack for doing this. I wrote this code :

    HtmlDocument doc = new HtmlDocument();
    doc.LoadHtml(result);
    HtmlNode node = doc.DocumentNode.SelectSingleNode("//*div[@class='item']"); 
    string value = node.InnerText; 

'result' is my content of the File. But i get this exception : 'Expression must evaluate to a node-set'

And this is some of mt file's content :

<div class="Clear" style="height:15px;"></div>
<div class='Container Select' id="Container_1">
<div class='Item'><div class='Part Lable'>موضوع : </div><div class='Part ...

Popular Answer

have you tried using XPath for example if I wanated to find a if a node is selected in my example I would do the following

string xpath = null;
XmlNode configNode = configDom.DocumentElement;
// collect selected nodes in node list
XmlNodeList nodeList =
configNode.SelectNodes(@"//*[@status='checked']");

in your case you would do the following

HtmlDocument doc = new HtmlDocument();
doc.LoadHtml(result);
HtmlNode node = doc.DocumentNode.SelectSingleNode("//*/div[@class='item']"); 
string value = node.InnerText; 


Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why