Reputation: 1
Following is the HTML i am working on; (C# project)
</TR>
<TR>
`<TD ALIGN="center"> <INPUT TYPE="checkbox" NAME="f0" VALUE="14652 4-76-17-7-2024-R" ONCLICK=uncheck("f0")> </TD>
<TD> </TD>
<TD>76/07-17</TD>
<TD>14652</TD>
<TD> </TD>
<TD>9.3</TD>
<TD> 78 </TD>
<TD> </TD>
<TD> </TH>
</TR>
I can get the tr[td] inner text via a loop.
But in the same loop I can not access the "input" attributes (Name / Value) to extract values as a array to maintain data integrity.
HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
doc.LoadHtml(page);
foreach (var row in doc.DocumentNode.SelectNodes("//tr[td]")) //textBox5.Text
{
table.Rows.Add(row.SelectNodes("td").Select(td => td.InnerText).ToArray());
string namee1 = row.InnerText;
//These works but without the attribute details.
string Code_num = row.SelectSingleNode("//td//input").Attributes["value"].Value;
// this gives only the first value without looping.
MessageBox.Show(namee);
}
Code_num - gives me the first value only without looping through the document.
Attributes dose not pick and I only get null reference. I am not that familiar with linq. Prefer help from xpath if possible.
Appreciate your help!!! Thank in Advance
Desired Out put as follows;
Col 1 / Col 2/ col3 / col 4 / col5 / col6 / col 7 / col 8 /col9 / col10 / col/11
  / /76/07-17/14652/ /9.3/ 78 / /  /F0 / 14652 4-76-17-7-2024-R
Upvotes: -1
Views: 37
Reputation: 1
The following worked for me! There was null reference blocking my code as earlier values are not complete.
Hope this helps someone!!
foreach (var row in doc.DocumentNode.SelectNodes("//tr[td]"))
{
string pos = "0";
pos = row.SelectSingleNode("td//input")?.Attributes["name"].Value;
string injid = row.SelectSingleNode("td//input")?.Attributes["value"].Value;
table.Rows.Add((row.SelectNodes("td").Select(td => td.InnerText).ToArray()));
if (string.IsNullOrEmpty(pos))
{
}
else
{
int ronum = table.Rows.Count - 1;
DataRow dr = table.Rows[ronum];
dr[9] = pos;
}
if (string.IsNullOrEmpty(injid))
{
}
else
{
int ronum = table.Rows.Count - 1;
DataRow dr = table.Rows[ronum];
dr[10] = injid;
}
}
Upvotes: 0