1

I have this html code:

<div class="searchResult webResult">

  <div class="resultTitlePane">
    Google
  </div>

  <div class="resultDisplayUrlPane">
    www.google.com  
  </div>

 <div class="resultDescription">
   Search
 </div>
</div>

I want to access innertext inside divs in diffrent variables
I know for accessing a div with a class I hould write

var titles = hd.DocumentNode.SelectNodes("//div[@class='searchResult webResult']"); 

foreach (HtmlNode node in titles)
    {?}

what code should I write to get the innertext of each dive in different variables.TNX

mary
  • 247
  • 3
  • 20

2 Answers2

2

I would extend the current XPath expression you have to match the inner div elements:

//div[@class='searchResult webResult']/div[contains(@class, 'result')]

Then, to get the text, use the .InnerText property:

Community
  • 1
  • 1
alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
0

Since you don't know how many nodes will be returned, I suggest using a list:

List<string> titlesStringList = new List<string>();
foreach (HtmlNode node in titles)
{
    titlesStringList.Add(node.InnerText);
}
Steve Wellens
  • 20,506
  • 2
  • 28
  • 69
  • TNX I will use list. but `titlesStringList.Add(node.InnerText);` just returns one of divs innertext . can I use if to get innertext of each divs seprately in different lists? – mary Jul 04 '16 at 14:53