Convert HTML Tables To DataSet In C#

  1. private DataSet ConvertHTMLTablesToDataSet(string HTML) {  
  2.     // Declarations  
  3.     DataSet ds = new DataSet();  
  4.     DataTable dt = null;  
  5.     DataRow dr = null;  
  6.     DataColumn dc = null;  
  7.     string TableExpression = "<TABLE[^>]*>(.*?)</TABLE>";  
  8.     string HeaderExpression = "<TH[^>]*>(.*?)</TH>";  
  9.     string RowExpression = "<TR[^>]*>(.*?)</TR>";  
  10.     string ColumnExpression = "<TD[^>]*>(.*?)</TD>";  
  11.     bool HeadersExist = false;  
  12.     int iCurrentColumn = 0;  
  13.     int iCurrentRow = 0;  
  14.     // Get a match for all the tables in the HTML  
  15.     MatchCollection Tables = Regex.Matches(HTML, TableExpression, RegexOptions.Multiline | RegexOptions.Singleline | RegexOptions.IgnoreCase);  
  16.     // Loop through each table element  
  17.     foreach(Match Table in Tables) {  
  18.         // Reset the current row counter and the header flag  
  19.         iCurrentRow = 0;  
  20.         HeadersExist = false;  
  21.         // Add a new table to the DataSet  
  22.         dt = new DataTable();  
  23.         //Create the relevant amount of columns for this table (use the headers if they exist, otherwise use default names)  
  24.         // if (Table.Value.Contains("<th"))  
  25.         if (Table.Value.Contains("<TH")) {  
  26.             // Set the HeadersExist flag  
  27.             HeadersExist = true;  
  28.             // Get a match for all the rows in the table  
  29.             MatchCollection Headers = Regex.Matches(Table.Value, HeaderExpression, RegexOptions.Multiline | RegexOptions.Singleline | RegexOptions.IgnoreCase);  
  30.             // Loop through each header element  
  31.             foreach(Match Header in Headers) {  
  32.                 dt.Columns.Add(Header.Groups[1].ToString());  
  33.             }  
  34.         } else {  
  35.             for (int iColumns = 1; iColumns <= Regex.Matches(Regex.Matches(Regex.Matches(Table.Value, TableExpression, RegexOptions.Multiline | RegexOptions.Singleline | RegexOptions.IgnoreCase)[0].ToString(), RowExpression, RegexOptions.Multiline | RegexOptions.Singleline | RegexOptions.IgnoreCase)[0].ToString(), ColumnExpression, RegexOptions.Multiline | RegexOptions.Singleline | RegexOptions.IgnoreCase).Count; iColumns++) {  
  36.                 dt.Columns.Add("Column " + iColumns);  
  37.             }  
  38.         }  
  39.         //Get a match for all the rows in the table  
  40.         MatchCollection Rows = Regex.Matches(Table.Value, RowExpression, RegexOptions.Multiline | RegexOptions.Singleline | RegexOptions.IgnoreCase);  
  41.         // Loop through each row element  
  42.         foreach(Match Row in Rows) {  
  43.                 // Only loop through the row if it isn't a header row  
  44.                 if (!(iCurrentRow == 0 && HeadersExist)) {  
  45.                     // Create a new row and reset the current column counter  
  46.                     dr = dt.NewRow();  
  47.                     iCurrentColumn = 0;  
  48.                     // Get a match for all the columns in the row  
  49.                     MatchCollection Columns = Regex.Matches(Row.Value, ColumnExpression, RegexOptions.Multiline | RegexOptions.Singleline | RegexOptions.IgnoreCase);  
  50.                     // Loop through each column element  
  51.                     foreach(Match Column in Columns) {  
  52.                             // Add the value to the DataRow  
  53.                             dr[iCurrentColumn] = Column.Groups[1].ToString();  
  54.                             // Increase the current column  
  55.                             iCurrentColumn++;  
  56.                         }  
  57.                         // Add the DataRow to the DataTable  
  58.                     dt.Rows.Add(dr);  
  59.                 }  
  60.                 // Increase the current row counter  
  61.                 iCurrentRow++;  
  62.             }  
  63.             // Add the DataTable to the DataSet  
  64.         ds.Tables.Add(dt);  
  65.     }  
  66.     return ds;  
  67. }