Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8788379
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T22:07:51+00:00 2026-06-13T22:07:51+00:00

I am using the following query to parse html table data. Dim q =

  • 0

I am using the following query to parse html table data.

Dim q = From table In htmldoc.DocumentNode.SelectNodes("//table[@class='Seller']").Cast(Of HtmlNode)()
                    From row In table.SelectNodes("tr").Cast(Of HtmlNode)()
                    From header In row.SelectNodes("th").Cast(Of HtmlNode)()
                    From cell In row.SelectNodes("td").Cast(Of HtmlNode)()
               Select New With {Key .Table = table.Id, Key .CellText = cell.InnerText, Key .headerText = header.InnerText}

How can i use for each loops how can to fill this into a datatable?

I would create columns first using the header data then use a nested for each loop to fill the cell data in the table, but i am not sure how to, also any suggested changes on the above LINQ query?

Note: The html page contains only one table always.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T22:07:52+00:00Added an answer on June 13, 2026 at 10:07 pm

    Given the following html

    Dim t = <table class='Seller' id='MyTable'>
                <tr>
                    <th>FooColumn</th>
                    <td>Foo</td>
                    <td>Another Foo</td>
                </tr>
                <tr>
                    <th>BarColumn</th>
                    <td>Bar</td>
                    <td>Another Bar</td>
                </tr>
                <tr>
                    <th>ThirdColumn</th>
                    <td>Third</td>
                    <td>Another Third</td>
                </tr>
            </table>
    
    Dim htmldoc = New HtmlAgilityPack.HtmlDocument()
    htmldoc.LoadHtml(t.ToString())
    

    and your query

    Dim q = From table In htmldoc.DocumentNode.SelectNodes("//table[@class='Seller']")
                From row In table.SelectNodes("tr")
                    From header In row.SelectNodes("th")
                    From cell In row.SelectNodes("td")
            Select New With {.Table = table.Id, .CellText = cell.InnerText, .headerText = header.InnerText}
    

    you can use GroupBy or ToLookup to group the objects by columns:

    Dim grouped = q.ToLookup(Function(a) a.headerText)
    

    and use this grouping to create a DataTable with the appropriate DataColumns:

    Dim dt = new DataTable()
    
    For Each h in grouped.Select(Function(g) g.Key)
        dt.Columns.Add(h)
    Next
    

    Now, for filling the DataTable, you have to “rotate” the grouping, since each group contains the data for one column, but we want the data for each row. Let’s use a little helper method

    Function Rotate(Of T, TR)(source As IEnumerable(Of IEnumerable(Of T)), 
                              selector As Func(Of IEnumerable(Of T), IEnumerable(Of TR))) As IEnumerable(Of IEnumerable(Of TR))
    
        Dim result = new List(Of IEnumerable(Of TR))
        Dim enums = source.Select(Function(e) e.GetEnumerator()).ToArray()
        While enums.All(Function(e) e.MoveNext())
            result.Add(selector(enums.Select(Function(e) e.Current)).ToArray())
        End While
    
        Return result
    End Function
    

    to fill the DataTable.

    For Each rrow in Rotate(grouped, Function(row) row.Select(Function(e) e.CellText))
        dt.Rows.Add(rrow.ToArray())
    Next 
    

    And now the DataTable will look like this:

    enter image description here

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using the following query to select users from my 'users' table with the
I am using the following query to select wait times from the table wait_times
I'm using the following query to extract the frequent short values from a mediumblob
i've been using the following query: select LEN(columnname) as columnmame from dbo.amu_datastaging This works,
I can retrieve individuals from my ontology using following query: SELECT ?indiv WHERE {
I am using DOMDocument to parse an html document and get some data out
I have the following html and i am using php's DomDocument class to get
I am using this query to fetch data from webserver onto my iPhone app
I am using following query which works fine for me except one problem SELECT
using the following query in SQLDeveloper to test merge everything goes fine: merge into

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.