Click here to Skip to main content
15,892,072 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,
I have this html with table.
I can get "col1" and "cal2" but I don't know how to get also value of "data-index", "data-name":

<table class="footable table" id="footable">
<tbody>
<tr class="trclass red" data-index="123" data-name="Apple">
<td class="col1" >Green</td>
<td class="col2" >1.25</td>
</td></tr>
</tbody>
</table>
HTML



Any help?

What I have tried:

// @Nuget: HtmlAgilityPack

using System;
using System.Xml;
using System.IO;
using HtmlAgilityPack;
					
public class Program
{
	public static void Main()
	{
		var html =
        @"<html>
		<tbody>
		<table id=\'footable\'>
			<tr class=\'trclass red\' data-index=\'123\' data-name=\'Apple\'>
			<td class=\'col1\' >Green</td>
			<td class=\'col2\' > 1.25</td></tr>
		</table>
		</tbody></html>";
		
		var htmlDoc = new HtmlDocument();
        htmlDoc.LoadHtml(html);
		
		var tbody = htmlDoc.DocumentNode.SelectNodes("//table[contains(@id, 'foo')]//tr//td");
	

		foreach(var nob in tbody)
		{
			 Console.Write(nob.InnerHtml);	

		}
	}
}
C#

Posted
Updated 6-May-19 1:49am
v3
Comments
[no name] 5-May-19 17:06pm    
"Tr" ending tag is missing.
Christian Graus 5-May-19 18:23pm    
No it isn't :)
[no name] 5-May-19 23:44pm    
			<tr class=\'trclass red\' data-index=\'123\' data-name=\'Apple\'>
			<td class=\'col1\' >Green</td>
			<td class=\'col2\' > 1.25</td>
		</table>
Christian Graus 5-May-19 23:50pm    
Wow, that had a this morning (on the same line), I was sure of it
[no name] 6-May-19 0:13am    
2 hours "after" I looked at it? And then it was changed back? You should be in the WH.

Use .Attributes on the node to read that nodes attributes like data-index etc

Select Nodes | Html Agility Pack[^]
 
Share this answer
 
Why not just read it as XML, given that it is?
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900