简体   繁体   中英

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

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 ...

try either

"//*/div[@class='item']"

or simply

"//div[@class='item']"

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; 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM