0

So, I have this pretty long html file, and I need to fill a listview with some of it's elements. I'm just starting off with HtmlAgilityPack, and I have no clue how to even start. My html is as follows:

<html>
<body>
  <div class="content_bg">
    <div style="width:868px; margin:0px auto">
      <div class="main">
        <div class="content">
          <center>
            <div class="content-1col-nobox">
              <div style="width:720px;">
                <table class="vlist">
                  <tbody>
                    <tr>
                      <td valign="top">
                        <div class="link">
                          <a href="http://link i need here/">Text I need here</a>
                        </div class="link">
                      </td>
                    </tr>
                  </tbody>
                </table>
              </div>
            </div>
          </center>
        </div>
      </div>
    </div>
  </div>
</body>
</html>

I have a ton of those "tr" elements, and I'm attempting to retrieve all of them using foreach, but that's not my problem. The problem is that I don't know how to get to there. For the moment, I'm trying to use this absolutely chaotic piece of code:

        using (HttpClient client = new HttpClient())
        {
            using (HttpResponseMessage response = await client.GetAsync(("http://vodlocker.com/?op=search&k=" + txtbox.Text)))
            {
                if (response.IsSuccessStatusCode)
                {
                    using (Stream stream = response.Content.ReadAsStreamAsync().Result)
                    {
                        HtmlDocument doc = new HtmlDocument();
                        doc.Load(stream);
                        var titleElement = doc.DocumentNode.Element("html").Element("body").Element("div class=\"content_bg\"").Element("div style=\"width: 868px; margin: 0px auto\"").Element("div class=\"main\"").Element("div class=\"content\"").Element("center").Element("div class=\"content - 1col - nobox\"").Element("div style=\"width: 720px;\"").Element("table class=\"vlist\"").Element("tbody");
                    }
                }
            }
        }

But, suprisingly, it isn't working. I get an "object reference not set to an instance of an object" error on the "var titleElement" line.

user2950509
  • 1,018
  • 2
  • 14
  • 37
  • Have you tried `doc.DocumentNode.SelectNodes("//a")`? – Robert Harvey Nov 16 '15 at 20:49
  • If you find duplicate not enough - make sure to search first - https://www.bing.com/search?q=c%23+select+htmlagilitypack and update your post with what you tried (and look somewhat reasonable) so question can be re-opened. Additionally consider to read something on XPath - [MSDN XPath Examples](https://msdn.microsoft.com/en-us/library/ms256086(v=vs.110).aspx) has good cheat-sheet. – Alexei Levenkov Nov 16 '15 at 20:51

0 Answers0