0

This is part of html that i am parsing

<li><a href="http://some.link.com/4DFR6DJ43Y/sessionid?ticket=ASDSIDFK32423421" target="_blank">http://some.link.com/4DFR6DJ43Y/sessionid?ticket=ASDSIDFK32423421</a></li>

I want to get http://some.link.com/4DFR6DJ43Y/sessionid?ticket=ASDSIDFK32423421 as an output.

So far i have tried

        HtmlDocument document = new HtmlDocument();
        document.LoadHtml(responseFromServer);


        var link = document.DocumentNode.SelectSingleNode("//a");

        if (link != null)
        {  
            if(link.innerText.Contains("ticket"))
            {
                Console.WriteLine(link.InnerText);
            }
        }

... but output is null (no inner texts are found).

Tyress
  • 3,573
  • 2
  • 22
  • 45
Tagyoureit
  • 359
  • 1
  • 5
  • 18

3 Answers3

1

That's probably because the first link in your HTML document as returned by SelectSingleNode(), doesn't contains text "ticket". You can check for the target text in XPath directly , like so :

var link = document.DocumentNode.SelectSingleNode("//a[contains(.,'ticket')]");

if (link != null)
{
    Console.WriteLine(link.InnerText);
}

or using LINQ style if you like :

var link = document.DocumentNode
                   .SelectNodes("//a")
                   .OfType<HtmlNode>()
                   .FirstOrDefault(o => o.InnerText.Contains("ticket"));

if (link != null)
{
    Console.WriteLine(link.InnerText);
}
har07
  • 88,338
  • 12
  • 84
  • 137
1

You provided a piece of code that won't compile because innerText is not defined. If you try this code, you'll probably get what you asked for:

HtmlDocument document = new HtmlDocument();
document.LoadHtml(html);

var link = document.DocumentNode.SelectSingleNode("//a");

if (link != null)
{
    if(link.InnerText.Contains("ticket"))
    {
        Console.WriteLine(link.InnerText);
    }
}
Serhiy Chupryk
  • 438
  • 2
  • 5
  • 15
  • Thank you its a typo when copy/pasting. This returns NULL tho. I have settled on Xpath solution posted above. Thank you for your time. – Tagyoureit Feb 17 '16 at 06:47
0

You can use HTML Agility Pack instead of HTML Document then you can do deep parsing in HTML. for more information please see the following information. See the following link. How to use HTML Agility pack

Community
  • 1
  • 1