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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T16:16:39+00:00 2026-05-23T16:16:39+00:00

I’m using HtmlAgilityPack to obtain some Html from a web site. Here is the

  • 0

I’m using HtmlAgilityPack to obtain some Html from a web site.

Here is the received Html:

<table class="table">
<tr>
    <td>
        <table class="innertable">...</table>
    </td>
</tr>
<tr>
    <td colspan="2"><strong>Contact</strong></td>
</tr>
<tr>
    <td colspan="2">John Doe</td>
</tr>
<tr>
    <td colspan="2">Jane Doe</td>
</tr>
<tr>
    <td colspan="2">&nbsp;</td>
</tr>
<tr>
    <td><strong>Units</strong></td>
    <td>32</td>
</tr>
<tr>
    <td><strong>Year</strong></td>
    <td>1998</td>
</tr>
</table>

The Context:

I’m using the following code to get the first :

var table = document.DocumentNode.SelectNodes("//table[@class='table']").FirstOrDefault();

I’m using the following code to get the inner table :

var innerTable = table.SelectNodes("//table[@class=innertable]").FirstOrDefault();

So far so good!

I need to get some information from the first table and some from the inner table.
Since I begin with the information from the first table I need to skip the first row (which holds the inner table) so I do the following:

var tableCells = table.SelectNodes("tr[position() > 1]/td");

Since I now have all the cells from the first table excluding the inner table, I start doing the following:

string contact1 = HttpUtility.HtmlDecode(tableCells[1].InnerHtml);
string contact2 = HttpUtility.HtmlDecode(tableCells[2].InnerHtml);

string units = HttpUtility.HtmlDecode(tableCells[5].InnerHtml);
string years = HttpUtility.HtmlDecode(tableCells[7].InnerHtml);

The problem:

I’m getting the values I want by hardcoding the index in tableCells[] not thinking the layout would move…unfortunately, it does move.

In some cases I do not have a “Jane Doe” row (as shown in the above Html), this means I may or may not have two contacts.

Because of this, I can’t hardcode the indexes since I might end up having the wrong data in the wrong variables.

So I need to change my approach…

Does anyone know how I could perfect my algorithm so that it can take into account the fact that I may have one or two contacts and perhaps not use hardcoded indexes?

Thanks in advance!

vlince

  • 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-05-23T16:16:39+00:00Added an answer on May 23, 2026 at 4:16 pm

    There is never one unique solution to this kind of problem. Here is an XPATH that seems to do some kind of it though:

            HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
            doc.Load(yourHtmlFile);
    
            doc.Save(Console.Out);
    
            foreach (HtmlNode node in doc.DocumentNode.SelectNodes("//tr[td/strong/text() = 'Contact']/following-sibling::tr/td/text()[. != '&nbsp;']"))
            {
                Console.WriteLine(node.OuterHtml);
            }
    

    will display this:

    John Doe
    Jane Doe
    32
    1998
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hi I am using HtmlAgilityPack to scrap some data from web using c# .
I am using the HtmlAgilityPack to retrieve information from a web page and, at
I am using HtmlAgilityPack to read a parse a html file and extract some
I'm using the HTMLAgilityPack to parse HTML pages. However at some point I try
I'm trying to extract text from HTML using HtmlAgilityPack. I successfully added HtmlAgilityPack to
here's my sample HTML... <html> <table class=test border=0 > <tr bgColor=#e8f4ff> <td width=50% align=right>
I've been using the HtmlAgilityPack to eat some XHTML documents, however, if I want
Can I put OR clause in node selection using HTMLAgility (HtmlAgilityPack.HtmlNodeCollection)doc.DocumentNode.SelectNodes(//td[@class=\roomPrice figure\]); What I
I need to extract text from an HTML file using C#. I am trying
I'm using HtmlAgilityPack to parse HTML. I want to check if an element has

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.