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 8448101
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T10:20:14+00:00 2026-06-10T10:20:14+00:00

In the following HTML, I can parse the table element, but I don’t know

  • 0

In the following HTML, I can parse the table element, but I don’t know how to skip the th elements.

I want to get only the td elements, but when I try to use:

foreach (HtmlNode cell in row.SelectNodes("td"))

…I get an exception.

<table class="tab03">
  <tbody>
    <tr>
      <th class="right" rowspan="2">first</th>
    </tr>
    <tr>
      <th class="right">lp</th>
      <th class="right">name</th>
    </tr>
    <tr>
      <td class="right">1</td>
      <td class="left">house</td>
    </tr>
    <tr>
      <th class="right" rowspan="2">Second</th>
    </tr>
    <tr>
      <td class="right">2</td>
      <td class="left">door</td>
    </tr>
  </tbody>
</table>

My code:

var document = doc.DocumentNode.SelectNodes("//table");
string store = "";

if (document != null)
{
    foreach (HtmlNode table in document)
    {
        if (table != null)
        {
            foreach (HtmlNode row in table.SelectNodes("tr"))
            {
                store = "";
                foreach (HtmlNode cell in row.SelectNodes("th|td"))
                {
                    store = store + cell.InnerText+"|";
                }

                sw.Write(store );
                sw.WriteLine();  
            }
        }
    }
}

sw.Flush();
sw.Close(); 
  • 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-10T10:20:15+00:00Added an answer on June 10, 2026 at 10:20 am

    This method uses LINQ to query for HtmlNode instances that have the name td.

    I also noticed your output appears as val|val| (with the trailing pipe), This sample uses string.Join(pipe, array) as a less-hideous method of removing that trailing pipe: val|val.

    using System.Linq;
    
    // ...
    
    var tablecollection = doc.DocumentNode.SelectNodes("//table");
    string store = string.Empty;
    
    if (tablecollection != null)
    {
        foreach (HtmlNode table in tablecollection)
        {
            // For all rows with at least one child with the 'td' tag.
            foreach (HtmlNode row in table.DescendantNodes()
                .Where(desc =>
                    desc.Name.Equals("tr", StringComparison.OrdinalIgnoreCase) &&
                    desc.DescendantNodes().Any(child => child.Name.Equals("td",
                        StringComparison.OrdinalIgnoreCase))))
            {
                // Combine the child 'td' elements into an array, join with the pipe
                // to create the output in 'val|val|val' format.
                store = string.Join("|", row.DescendantNodes().Where(desc =>
                    desc.Name.Equals("td", StringComparison.OrdinalIgnoreCase))
                    .Select(desc => desc.InnerText));
    
                // You can probably get rid of the 'store' variable as it's
                // no longer necessary to store the value of the table's
                // cells over the iteration.
                sw.Write(store);
                sw.WriteLine();
            }
        }
    }
    
    sw.Flush();
    sw.Close(); 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following HTML that can appear but only one at a time:
I have the following html structure (which is generated and I can't change): <table
I have the following html and css. But what I can't figure out is
I want to get 'second' in the following sample web address. http://www.mywebsite.com/first/sedond/third.html first can
I am trying to parse the following HTML. I need to get the innertext
I am using mechanize/nokogiri and need to parse out the following HTML string. can
Ok, so I can parse the XML with jQuery but it only seems to
I want to parse the second div from the following HTML: <div kubedfiuabefiudsabiubfg><div class='post-body
I have the following HTML and you can see the extra space between the
I have the following razor template html, although I can't figure out how to

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.