Reputation: 793
i have an html file source with a table that holds values, i want to transfer each value into a data grid view that i build. example :
<tr><td>32</td><td>jon</td><td>smith</td></tr>
and in my data grid view i want:
age firstName lastName
32 jon smith
(i allready build a grid with column heads) and so on...
how can i "fish" the values from each td into the right place? tnx :)
Upvotes: 1
Views: 2925
Reputation: 50184
If your HTML is really this simple, with no attributes on the rows or cells, and no funny characters in the contents, a regular expression is a quick and dirty solution:
string html = "<table><tr><td>32</td><td>jon</td><td>smith</td></tr></table>";
string pattern = "<tr>(?:<td>(.*?)</td>)*?</tr>";
foreach (Match m in Regex.Matches(html, pattern, RegexOptions.IgnoreCase))
{
// Add row
var row = grid.AddRow();
foreach (Capture c in m.Groups[1].Captures)
{
// Add cell
var cell = row.AddCell();
cell.Contents = c.Value;
}
}
If there's any chance of your HTML not being this simple, use the HTML Agility Pack:
string html = "<table><tr><td>32</td><td>jon</td><td>smith</td></tr></table>";
var table = new HtmlDocument();
table.LoadHtml(html);
foreach (var tr in table.DocumentNode.Descendants("tr"))
{
// Add row
var row = grid.AddRow();
foreach (var td in tr.Descendants("td"))
{
var cell = row.AddCell();
cell.Contents = td.InnerText;
}
}
(I've guessed at how you actually add rows and cells here; hopefully you can fix that up for yourself.)
Upvotes: 0
Reputation: 125
A dirty version. Instead of List<IList>
you can generate your own datasource. Beware of malformed HTML:
class Program {
static Regex cell = new Regex("<td>(.+?)</td>", RegexOptions.IgnoreCase);
static string htmlTable = "<tr><td>32</td><td>41</td></tr><tr><td>123123</td><td>123123123</td></tr>";
static void Main(string[] args) {
var table = new List<IList<string>>(); //list of lists, kind of data table
foreach (var rowString in htmlTable.Split(new [] { "</tr>" }, StringSplitOptions.RemoveEmptyEntries)) //split into rows
table.Add(GetRowValues(rowString)); //get and add row values
}
static IList<string> GetRowValues(string rowString) {
return new List<string>(cell.Matches(rowString).Cast<Match>().Select(m => m.Groups[1].Value)); //extract cells values by regex
}
}
Upvotes: 0
Reputation: 22008
Robust solution - by parsing. Read html file into a string, then read it by character into another string, when you read >
, then you should have a whole tag read(<tr>
,</tr>
,<td>
or </td>
), check which one, if it's </tr>
, then set X=0,Y++, if it's </td>
, then X++, after reading tag - clear text, repeat, if you read <
and there are some data read already, then this data is a text what should be put into [X,Y] position in data grid (which you can init after parsing, just find maximum X to know number of columns)...
.. i hope there will be no difficulties to put said into C# code =D
Upvotes: 2
Reputation: 9610
Are these values already in a DataTable or DataSet of some kind? The GridView in itself renders as a table, so are you trying to do something unnecessarily?
Okay I see you've amended the question. There is something called the HTML Agility pack that can parse HTML and extract values. Not used it myself though but it would save you parsing the table code as a string, providing that your HTML table is valid HTML.
http://htmlagilitypack.codeplex.com/
Upvotes: 1