title: C# extract content of a div from a string
body: <p>I need to extract the content tr and td of a 2nd table inside the div from the external url. I can't use HtmlAglitityPack.</p>
  
  <p>Design is something like this:</p>
  
  <pre><code>&lt;div class="class1" id="content-main"&gt;
        &lt;table width="90%"&gt;
          &lt;tbody&gt;
            &lt;tr&gt;&lt;td class="table_left_corner"&gt;&amp;nbsp;&lt;/td&gt;&lt;td class="table_head"&gt;table1 &lt;/td&gt;&lt;td class="table_right_corner"&gt;&amp;nbsp;&lt;/td&gt;&lt;/tr&gt;
          &lt;/tbody&gt;
        &lt;/table&gt;
        &lt;table width="90%"&gt;
          &lt;tbody&gt;
            &lt;tr&gt;&lt;td class="table_left_corner"&gt;&amp;nbsp;&lt;/td&gt;&lt;td class="table_head"&gt;table2&lt;/td&gt;&lt;td class="table_right_corner"&gt;&amp;nbsp;&lt;/td&gt;&lt;/tr&gt;
          &lt;/tbody&gt;
        &lt;/table&gt;
        &lt;table width="90%"&gt;
          &lt;tbody&gt;
            &lt;tr&gt;&lt;td class="table_left_corner"&gt;&amp;nbsp;&lt;/td&gt;&lt;td class="table_head"&gt;table3 &lt;/td&gt;&lt;td class="table_right_corner"&gt;&amp;nbsp;&lt;/td&gt;&lt;/tr&gt;
          &lt;/tbody&gt;
        &lt;/table&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>So I want to use some Regex functions to return the content of a table.</p>
  
  <pre><code> using (WebClient client = new WebClient())
   {
      string htmlcode= client.DownloadString("http://www.example.com");
  
      string r = @"&lt;div.*?id=""content-main"".*?&gt;.*&lt;/div&gt;";       
  
      Match match2 = Regex.Match(htmlcode, r);
  
      string a = match2.Groups[1].Value;
   }
  </code></pre>
  
  <p>I use different regex expression but all are failed. so please help. how can I get content of a 2nd table.</p>
  
  <p>Edit 2
  By using HTMLAglityPack</p>
  
  <pre><code>    var web = new HtmlWeb();
      var document = web.Load("http://www.example.com/");
      var page = document.DocumentNode;
  
  
  string outerHTML = page.SelectNodes("//table")[5].OuterHtml;
      Match match1;
      match1 = Regex.Match(outerHTML, @"&lt;a [^&gt;]+&gt;(.*?)&lt;\/a&gt;");
  
          while (match1.Success)
          {
              string NAme = match1.Groups[1].Value;                      
  
              var webloc = new HtmlWeb();
              dynamic documentloc = null;
              documentloc = webloc.Load(urlAddress + NAme.Replace(" ", "-").ToLower());
              dynamic pageloc = documentloc.DocumentNode;
  
              string outerHTMLloc = pageloc.SelectNodes("//table")[5].OuterHtml;
  
              match1 = match1.NextMatch();                               
          }
  </code></pre>
  
  <p>First time it run successfully but when second time come it throws an error on "outerHTMLloc"</p>
  
  <p>Error:"An unhandled exception of type 'System.StackOverflowException' occurred in HtmlAgilityPack.DLL" </p>