Note that there are some explanatory texts on larger screens.

plurals
  1. POHtmlAgilityPack and selecting Nodes and Subnodes
    text
    copied!<p>Hope somebody can help me.</p> <p>Let´s say I have a html document that contains multiple divs like this example:</p> <pre><code>&lt;div class="search_hit"&gt; &lt;span prop="name"&gt;Richard Winchester&lt;/span&gt; &lt;span prop="company"&gt;Kodak&lt;/span&gt; &lt;span prop="street"&gt;Arlington Road 1&lt;/span&gt; &lt;/div&gt; &lt;div class="search_hit"&gt; &lt;span prop="name"&gt;Ted Mosby&lt;/span&gt; &lt;span prop="company"&gt;HP&lt;/span&gt; &lt;span prop="street"&gt;Arlington Road 2&lt;/span&gt; &lt;/div&gt; </code></pre> <p>I´m using HtmlAgilityPack to get the html document. What i need to know is how can i get the spans for each "search_hit"-div?</p> <p>My first thought was something like this:</p> <pre><code>foreach (HtmlAgilityPack.HtmlNode node in doc.DocumentNode.SelectNodes("//div[@class='search_hit']")) { foreach (HtmlAgilityPack.HtmlNode node2 in node.SelectNodes("//span[@prop]")) { } } </code></pre> <p>Each div should be a object with the included spans as properties. I. e.</p> <pre><code>public class Record { public string Name { get; set; } public string company { get; set; } public string street { get; set; } } </code></pre> <p>And this List shall be filled then:</p> <pre><code>public List&lt;Record&gt; Results = new List&lt;Record&gt;(); </code></pre> <p>But the XPATH i´m using is not doing a search in the subnode as it should do. It seams that it searches the whole document again and again.</p> <p>I mean I already got it working in that way that i just get the the spans of the whole page. But then i have no relation between the spans and divs. Means: I don´t know anymore which span is related to which div.</p> <p>Does somebody know a solution? I already played around that much that i´m totally confused now :)</p> <p>Any help is appreciated!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload