1

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?

1
  • You could use a regex. Or an HTML parser. Commented Apr 2, 2016 at 3:37

2 Answers 2

3

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 }
Sign up to request clarification or add additional context in comments.

2 Comments

It works! thank you so much. However since I'm trying to read those data from website, how could make the var source refer to the website's so that it actually loads from the website?
The Anglesharp readme docs has an example of loading a site here: github.com/AngleSharp/AngleSharp#simple-demo
0

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.
} 

5 Comments

Thanks for the comment! Though I dont understand why you put if ( descendants.length ) there?
Just ensure we have elements in the descendants
Sorry for keep asking. So i have got element by tagname of "strong", in var descendants, but how can I get access to the information that's saved there(which will be the TG)?
Once you get the element just use element.value that should give you TG in this case.
I'm sorry but do you mean use it like: descendants.value ?? Thank you so much for your answer :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.