Reputation: 6973
I have an html table
<table border="0" width="100%">
<tr class="headerbg">
<th width="5%">
No
</th>
<th width="30%">
Name
</th>
<th width="20%">
Department or Division
</th>
<th width="25%">
Email
</th>
<th width="20%">
Staff/Student
</th>
</tr>
<tr class="bg2">
<td>
1
</td>
<td>
<strong><a class="searchLink2" href="tel_search.php?fore=Dave&sur=Rumber">Dave Rumber</a></strong>
</td>
<td>
Medical School
</td>
<td>
<a class="searchLink2" href="mailto:[email protected]">[email protected]</a>
</td>
<td>
Student
</td>
</tr>
</table>
Sometimes there will be more than one row of people results. I would like to be able to go through each row and pluck out the name and e-mail information and do some other processing. Put the data in a datagrid, and possibly into a database.
I guess my question is how do I do this?
string table = GetContents(buffer);
table = table.Replace(" ", "");
table = table.Replace("&", "&");
XElement inters = XElement.Parse(table);
I can put it into an XElement but I am not quite sure where to go from here!
Thanks!
Upvotes: 1
Views: 1155
Reputation: 110221
Here's some freehand code that should get you started. Don't do this in production, this is an educational demonstration only.
List<XElement> rows = inters
.Descendants
.Where(x => x.Name == "tr")
.Skip(1) //header
.ToList();
//
// and now to turn rows into people
List<Person> people = rows
//filter to anchor. should be two.
.Select(r => r.Descendants.Where(a => a.Name = "a"))
//Project each anchor pair into a Person
.Select(g => new Person()
{
Name = g.First().Value,
Email = g.Skip(1).First().Value
})
.ToList();
Upvotes: 1
Reputation: 57872
You can actually use an HTML table as a data source for OLE DB:
http://connectionstrings.com/html-table
Full Disclosure: I haven't actually tried this - but I'm guessing it'll be much easier than trying to parse XML out of HTML.
Upvotes: 1