No...">

How do I make LINQ to XML and HTML work together?

I have 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&amp;sur=Rumber">Dave Rumber</a></strong>
            </td>
            <td>
                Medical School
            </td>
            <td>
                <a class="searchLink2" href="mailto:Dave.Rumber@Home.com">Dave.Rumber@Home.com</a>
            </td>
            <td>
                Student&nbsp;
            </td>
        </tr>
    </table>

      

Sometimes there are more than one row of results. I would like to be able to go through each line and rip out the name and email information and do some other processing. Put data in datagrid and possibly database.

I guess my question is, how should I do this?

  string table = GetContents(buffer);

  table = table.Replace("&nbsp;", "");
  table = table.Replace("&", "&amp;");

  XElement inters = XElement.Parse(table);

      

I can put it in an XElement, but I'm not really sure where to go from here!

Thank!

+1


source to share


2 answers


Here's some free hand code that should get you started. Don't do this in production, this is just an educational demonstration.



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();

      

+1


source


You can actually use HTML table as data source for OLE DB:

http://connectionstrings.com/html-table



Full disclosure: I haven't actually tried this, but I imagine it will be much easier than trying to parse XML from HTML.

+1


source







All Articles