Jack J
Jack J

Reputation: 67

C# accessing html element without id

I've been trying various different methods(webclient, webrequest, etc) to do this however I could not do it after all.

<td class="period_slot_1">
<strong>TG</strong>

What I want is to get access to the text value of above, so that I will get "TG" as result.

How can I do that?

Upvotes: 1

Views: 2919

Answers (2)

jdphenix
jdphenix

Reputation: 15415

I'm a fan of AngleSharp. Here's a minimal example using your HTML snippet.

        static void Main(string[] args)
        {
            var source = @"
<html>
<head>
</head>
<body>
    <td class=""period_slot_1"">
    <strong>TG</strong>
</body>
</html>";

            var parser = new HtmlParser();
            var document = parser.Parse(source);
            var strong = document.QuerySelector("strong"); 

            Console.WriteLine(strong.TextContent);
        }

The QuerySelector() method takes a CSS selector, so you can of course select "strong" without difficulty.

If you're aiming to get data out of a table and the class matters, you can (again, using normal DOM from JavaScript you're likely used to) use the DOM and LINQ to project the classes and data, like

            static void Main(string[] args)
            {
                var source = @"
<table>
    <tr>
        <td class=""period_slot_1"">
            <strong>TG</strong>
        </td>
        <td class=""period_slot_2"">
            <strong>TH</strong>
        </td>
        <td class=""period_slot_3"">
            <strong>TJ</strong>
        </td>
    </tr>
    <tr>
        <td class=""period_slot_1"">
            <strong>YG</strong>
        </td>
        <td class=""period_slot_2"">
            <strong>YH</strong>
        </td>
        <td class=""period_slot_3"">
            <strong>YJ</strong>
        </td>
    </tr>
</table>";

                var parser = new HtmlParser();
                var document = parser.Parse(source);
                var strongs = document.QuerySelectorAll("td > strong")
                    .Select(x => new
                    {
                        Class = x.ParentElement.ClassName,
                        Data = x.TextContent
                    })
                    .OrderBy(x => x.Class);

                strongs.ToList().ForEach(Console.WriteLine);
            }

outputs:

{ Class = period_slot_1, Data = TG }
{ Class = period_slot_1, Data = YG }
{ Class = period_slot_2, Data = TH }
{ Class = period_slot_2, Data = YH }
{ Class = period_slot_3, Data = TJ }
{ Class = period_slot_3, Data = YJ }

Upvotes: 3

Hari Prasad
Hari Prasad

Reputation: 16956

Use getElementsByClassName to identify parent and then look for descendants.

var parent = getElementsByClassName("period_slot_1")
var descendants = parent.getElementsByTagName("strong");
if ( descendants.length )
{
    // logic goes here.
} 

Upvotes: 0

Related Questions