JRB
JRB

Reputation: 247

xpath and htmlagility pack

I figured it out! I will leave this posted just in case some other newbie like myself has the same question.

Answer: **("./td[2]/span[@class='smallfont']")***

I am a novice at xpath and html agility. I am so close yet so far.

GOAL: to pull out 4:30am

by using the following with htmlagility pack:

foreach (HtmlNode table in doc.DocumentNode.SelectNodes("//table[@id='weekdays']/tr[2]")){
string time = table.SelectSingleNode("./td[2]").InnerText;

I get it down to "\r\n\t\t\r\n\t\t\t4:30am\r\n\t\t\r\n\t" when I try doing anything with the span I get xpath exceptions. What must I add to the ("./td[2]") to just end up with the 4:30am?

HTML
<td class="alt1 espace" nowrap="nowrap" style="text-align: center;">
<span class="smallfont">4:30am</span>
</td>

Upvotes: 1

Views: 403

Answers (1)

Nixie
Nixie

Reputation: 37

I don't know if Linq is an option, but you could have also done something like this:

        var time = string.Empty;
        var html =
            "<td class=\"alt1 espace\" nowrap=\"nowrap\" style=\"text-align: center;\"><span class=\"smallfont\">4:30am</span></td>";

        var document = new HtmlDocument() { OptionWriteEmptyNodes = true, OptionOutputAsXml = true };

        document.LoadHtml(html);

        var timeSpan =
            document.DocumentNode.Descendants("span").Where(
                n => n.Attributes["class"] != null && n.Attributes["class"].Value == "smallfont").FirstOrDefault();

        if (timeSpan != null)
            time = timeSpan.InnerHtml;

Upvotes: 0

Related Questions