title: Get HTML values from web response
body: <p>I am trying to parse an HTML response for a couple of values and then insert them into SQL.  I am able to get both values but, because the code is wrapped in a foreach statement, I get them twice. </p>
  
  <p>Here is my HTML response</p>
  
  <pre><code>&lt;div align="CENTER" class='dataTitle'&gt;Host State Breakdowns:&lt;/div&gt;
  &lt;p align='center'&gt;
  &lt;a href='trends.cgi?host=hostname&amp;includesoftstates=no&amp;assumeinitialstates=yes&amp;initialassumedhoststate=0&amp;backtrack=4'&gt;&lt;img src='trends.cgi?createimage&amp;host=hostname&amp;includesoftstates=no&amp;initialassumedhoststate=0&amp;backtrack=4' border="1" alt='Host State Trends' title='Host State Trends' width='500' height='20'&gt;&lt;/a&gt;&lt;br&gt;
  &lt;/p&gt;
  &lt;div align="CENTER"&gt;
  &lt;table border="0" class='data'&gt;
  &lt;tr&gt;&lt;th class='data'&gt;State&lt;/th&gt;&lt;th class='data'&gt;Type / Reason&lt;/th&gt;&lt;th class='data'&gt;Time&lt;/th&gt;&lt;th class='data'&gt;% Total Time&lt;/th&gt;&lt;th class='data'&gt;% Known Time&lt;/th&gt;&lt;/tr&gt;
  &lt;tr class='dataEven'&gt;&lt;td class='hostUP' rowspan="3"&gt;UP&lt;/td&gt;&lt;td class='dataEven'&gt;Unscheduled&lt;/td&gt;&lt;td class='dataEven'&gt;0d 10h 5m 19s&lt;/td&gt;&lt;td class='dataEven'&gt;100.000%&lt;/td&gt;&lt;td class='dataEven'&gt;100.000%&lt;/td&gt;&lt;/tr&gt;
  &lt;tr class='dataEven'&gt;&lt;td class='dataEven'&gt;Scheduled&lt;/td&gt;&lt;td class='dataEven'&gt;0d 0h 0m 0s&lt;/td&gt;&lt;td class='dataEven'&gt;0.000%&lt;/td&gt;&lt;td class='dataEven'&gt;0.000%&lt;/td&gt;&lt;/tr&gt;
  &lt;tr class='hostUNREACHABLE'&gt;&lt;td class='hostUNREACHABLE'&gt;Total&lt;/td&gt;&lt;td class='hostUNREACHABLE'&gt;0d 0h 0m 0s&lt;/td&gt;&lt;td class='hostUNREACHABLE'&gt;0.000%&lt;/td&gt;&lt;td class='hostUNREACHABLE'&gt;0.000%&lt;/td&gt;&lt;/tr&gt;
  &lt;tr class='dataOdd'&gt;&lt;td class='dataOdd' rowspan="3"&gt;Undetermined&lt;/td&gt;&lt;td class='dataOdd'&gt;Nagios Not Running&lt;/td&gt;&lt;td class='dataOdd'&gt;0d 0h 0m 0s&lt;/td&gt;&lt;td class='dataOdd'&gt;0.000%&lt;/td&gt;&lt;td class='dataOdd'&gt;&lt;/td&gt;&lt;/tr&gt;
  &lt;tr class='dataOdd'&gt;&lt;td class='dataOdd'&gt;Insufficient Data&lt;/td&gt;&lt;td class='dataOdd'&gt;0d 0h 0m 0s&lt;/td&gt;&lt;td class='dataOdd'&gt;0.000%&lt;/td&gt;&lt;td class='dataOdd'&gt;&lt;/td&gt;&lt;/tr&gt;
  &lt;tr class='dataOdd'&gt;&lt;td class='dataOdd'&gt;Total&lt;/td&gt;&lt;td class='dataOdd'&gt;0d 0h 0m 0s&lt;/td&gt;&lt;td class='dataOdd'&gt;0.000%&lt;/td&gt;&lt;td class='dataOdd'&gt;&lt;/td&gt;&lt;/tr&gt;
  &lt;tr&gt;&lt;td colspan="3"&gt;&lt;/td&gt;&lt;/tr&gt;
  &lt;tr class='dataEven'&gt;&lt;td class='dataEven'&gt;All&lt;/td&gt;&lt;td class='dataEven'&gt;Total&lt;/td&gt;&lt;td class='dataEven'&gt;0d 10h 5m 19s&lt;/td&gt;&lt;td class='dataEven'&gt;100.000%&lt;/td&gt;&lt;td class='dataEven'&gt;100.000%&lt;/td&gt;&lt;/tr&gt;
  &lt;/table&gt;
  &lt;/div&gt;
  &lt;br&gt;&lt;br&gt;
  &lt;div align="CENTER" class='dataTitle'&gt;State Breakdowns For Host Services:&lt;/div&gt;
  &lt;div align="CENTER"&gt;
  &lt;table border="0" class='data'&gt;
  &lt;tr&gt;&lt;th class='data'&gt;Service&lt;/th&gt;&lt;th class='data'&gt;% Time OK&lt;/th&gt;&lt;th class='data'&gt;% Time Warning&lt;/th&gt;&lt;th class='data'&gt;% Time Unknown&lt;/th&gt;&lt;th class='data'&gt;% Time Critical&lt;/th&gt;&lt;th class='data'&gt;% Time Undetermined&lt;/th&gt;&lt;/tr&gt;
  &lt;tr class='dataOdd'&gt;&lt;td class='dataOdd'&gt;&lt;a href='avail.cgi?host=hostname&amp;service=servicename&amp;t1=1478498400&amp;t2=1478534719&amp;backtrack=4&amp;assumestateretention=yes&amp;assumeinitialstates=yes&amp;assumestatesduringnotrunning=yes&amp;initialassumedhoststate=0&amp;initialassumedservicestate=0&amp;show_log_entries&amp;showscheduleddowntime=yes&amp;rpttimeperiod=24x7'&gt;servicename&lt;/a&gt;&lt;/td&gt;&lt;td class='serviceOK'&gt;100.000% (100.000%)&lt;/td&gt;&lt;td class='serviceWARNING'&gt;0.000% (0.000%)&lt;/td&gt;&lt;td class='serviceUNKNOWN'&gt;0.000% (0.000%)&lt;/td&gt;&lt;td class='serviceCRITICAL'&gt;0.000% (0.000%)&lt;/td&gt;&lt;td class='dataOdd'&gt;0.000%&lt;/td&gt;&lt;/tr&gt;
  &lt;tr class='dataEven'&gt;&lt;td class='dataEven'&gt;&lt;a href='avail.cgi?host=hostname&amp;service=servicename2&amp;t1=1478498400&amp;t2=1478534719&amp;backtrack=4&amp;assumestateretention=yes&amp;assumeinitialstates=yes&amp;assumestatesduringnotrunning=yes&amp;initialassumedhoststate=0&amp;initialassumedservicestate=0&amp;show_log_entries&amp;showscheduleddowntime=yes&amp;rpttimeperiod=24x7'&gt;servicename2&lt;/a&gt;&lt;/td&gt;&lt;td class='serviceOK'&gt;100.000% (100.000%)&lt;/td&gt;&lt;td class='serviceWARNING'&gt;0.000% (0.000%)&lt;/td&gt;&lt;td class='serviceUNKNOWN'&gt;0.000% (0.000%)&lt;/td&gt;&lt;td class='serviceCRITICAL'&gt;0.000% (0.000%)&lt;/td&gt;&lt;td class='dataEven'&gt;0.000%&lt;/td&gt;&lt;/tr&gt;
  &lt;/table&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>Here is my code:</p>
  
  <pre><code>var response = (HttpWebResponse)request.GetResponse();
  var stream = response.GetResponseStream();
  HtmlDocument doc = new HtmlDocument();
  doc.Load(stream);
  
  foreach (HtmlNode node in doc.DocumentNode.SelectNodes("//table[@class]"))
  {
  
      foreach (HtmlNode node2 in node.SelectNodes("//td[@class = 'serviceOK']"))
      {
          var value = node2.InnerText;
      }
  
      foreach (HtmlNode node3 in node.SelectNodes("//a[contains(@href, 'avail.cgi')]"))
      {
          var name = node3.InnerText;
      }
  
  }
  </code></pre>
  
  <p>name shows the servicename and value shows the class serviceOK but it repeats itself again because of the first foreach.</p>
  
  <p>My results look like this:</p>
  
  <pre><code>100.000% (100.000%)
  100.000% (100.000%)
  servicename
  servicename2
  100.000% (100.000%)
  100.000% (100.000%)
  servicename
  servicename2
  </code></pre>
  
  <p>Is there a way to, first, match the values up, and two, only have them show once?</p>