title: Parsing tables, cells with Html agility in C#
body: <p>I need to parse Html code. More specifically, parse each  cell of every  rows in all tables. Each row represent a single object and each cell represent different properties. I want to parse these to be able to write an XML file with every data inside (without the useless HTML code). I have successfully been able to parse each column from the HTML file but now I don't know what my options are for writing this to an XML file. I am baffled.</p>
  
  <p>HTML:</p>
  
  <pre><code>&lt;tr&gt;&lt;tr&gt; 
  &lt;td class="statBox" style="border-width:0px 1px 1px 0px; background-color: #FFFFFF"&gt; 
      1
  &lt;/td&gt; 
      &lt;td class="statBox" style="border-width:0px 1px 1px 0px; background-color: #FFFFFF" align="left"&gt; 
          &lt;a href="/ice/player.htm?id=8471675"&gt;Sidney Crosby&lt;/a&gt; 
      &lt;/td&gt; 
      &lt;td class="statBox" style="border-width:0px 1px 1px 0px; background-color: #FFFFFF" align="center"&gt; 
          PIT
      &lt;/td&gt; 
      &lt;td class="statBox" style="border-width:0px 1px 1px 0px; background-color: #FFFFFF" align="center"&gt; 
          C
      &lt;/td&gt; 
      &lt;td class="statBox" style="border-width:0px 1px 1px 0px; background-color: #FFFFFF" align="right"&gt; 
          39
      &lt;/td&gt; 
      &lt;td class="statBox" style="border-width:0px 1px 1px 0px; background-color: #FFFFFF" align="right"&gt; 
          32
      &lt;/td&gt; 
      &lt;td class="statBox" style="border-width:0px 1px 1px 0px; background-color: #FFFFFF" align="right"&gt; 
          33
      &lt;/td&gt; 
      &lt;td class="statBox sorted" style="border-width:0px 1px 1px 0px; background-color: #E0E0E0" align="right"&gt; 
          &lt;font color="#000000"&gt; 
              65
          &lt;/font&gt; 
      &lt;/td&gt; 
      &lt;td class="statBox" style="border-width:0px 1px 1px 0px; background-color: #FFFFFF" align="right"&gt; 
          20
      &lt;/td&gt; 
      &lt;td class="statBox" style="border-width:0px 1px 1px 0px; background-color: #FFFFFF" align="right"&gt; 
          29
      &lt;/td&gt; 
      &lt;td class="statBox" style="border-width:0px 1px 1px 0px; background-color: #FFFFFF" align="right"&gt; 
          10
      &lt;/td&gt; 
      &lt;td class="statBox" style="border-width:0px 1px 1px 0px; background-color: #FFFFFF" align="right"&gt; 
          1
      &lt;/td&gt; 
      &lt;td class="statBox" style="border-width:0px 1px 1px 0px; background-color: #FFFFFF" align="right"&gt; 
          3
      &lt;/td&gt; 
      &lt;td class="statBox" style="border-width:0px 0px 1px 0px; background-color: #FFFFFF" align="right"&gt; 
      &lt;/td&gt; 
      &lt;td class="statBox" style="border-width:0px 1px 1px 0px; background-color: #FFFFFF" align="right"&gt; 
          0
      &lt;/td&gt; 
      &lt;td class="statBox" style="border-width:0px 1px 1px 0px; background-color: #FFFFFF" align="right"&gt; 
          154
      &lt;/td&gt; 
      &lt;td class="statBox" style="border-width:0px 1px 1px 0px; background-color: #FFFFFF" align="right"&gt; 
          20.8
      &lt;/td&gt; 
      &lt;td class="statBox" style="border-width:0px 1px 1px 0px; background-color: #FFFFFF" align="right"&gt; 
          21:54
      &lt;/td&gt; 
      &lt;td class="statBox" style="border-width:0px 1px 1px 0px; background-color: #FFFFFF" align="right"&gt; 
          22.6
      &lt;/td&gt; 
      &lt;td class="statBox" style="border-width:0px 0px 1px 0px; background-color: #FFFFFF" align="right"&gt; 
          55.7
      &lt;/td&gt; 
  &lt;/tr&gt;&lt;/tr&gt;
  </code></pre>
  
  <p>C#:</p>
  
  <pre><code>using HtmlAgilityPack;
  
  namespace Stats
  {
      class StatsParser
      {
          private string htmlCode;
          private static string fileName = "[" + DateTime.Now.ToShortDateString() + " NHL Stats].xml";
  
          public StatsParser(string htmlCode)
          {
              this.htmlCode = htmlCode;
              this.ParseHtml();
          }
  
          public void ParseHtml()
      {
          HtmlDocument doc = new HtmlDocument();
          doc.LoadHtml(htmlCode);
  
          try
          {
              // Get all tables in the document
              HtmlNodeCollection tables = doc.DocumentNode.SelectNodes("//table");
  
              // Iterate all rows in the first table
              HtmlNodeCollection rows = tables[0].SelectNodes(".//tr");
              for (int i = 0; i &lt; rows.Count; ++i)
              {
  
                  // Iterate all columns in this row
                  HtmlNodeCollection cols = rows[i].SelectNodes(".//td[@class='statBox']");
                  for (int j = 0; j &lt; cols.Count; ++j)
                  {
  
                      // Get the value of the column and print it
                      string value = cols[j].InnerText;
                      if (value!="")
                          System.Windows.MessageBox.Show(value);
                  }
              }
          }
          catch (NullReferenceException)
          {
              System.Windows.Forms.MessageBox.Show("Exception!!");
          }
      }
  </code></pre>
  
  <p>XML:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8" ?&gt;
  
  &lt;Stats Date="2011-01-01"&gt;
    &lt;Player Rank="1"&gt;
      &lt;Name&gt;Sidney Crosby&lt;/Name&gt;
      &lt;Team&gt;PIT&lt;/Team&gt;
      &lt;Position&gt;C&lt;/Position&gt;
      &lt;GamesPlayed&gt;39&lt;/GamesPlayed&gt;
      &lt;Goals&gt;32&lt;/Goals&gt;
      &lt;Assists&gt;33&lt;/Assists&gt;
    &lt;/Player&gt;
  &lt;/Stats&gt;
  </code></pre>