Html Agility Pack selectnodes returns null

c# html-agility-pack html-parsing

Question

I'm trying to parse an HTML page using the HTML Agility Pack from http://www.livescience.com/41480-3d-printed-kidneys-take-small-steps.html on a Windows Phone 8 device. I have access to<title> tag quickly, but now I'd want to acquire the whole<p> tags taken from that file. I've tried one and two, but they didn't work. This is the real code I created based on their answers.

private void loadDoc()
    {
        try
        {
            HtmlWeb.LoadAsync("http://www.livescience.com/41480-3d-printed-kidneys-take-small-steps.html", Html_Completed);
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.ToString());
        }
    }

    private void Html_Completed(object sender, HtmlDocumentLoadCompleted e)
    {
        doc = e.Document;
        title = doc.DocumentNode.SelectSingleNode("//title");
        p = doc.DocumentNode.SelectNodes("//p");
        foreach(var node in p)
        {
          pr = node.InnerText; //that's the text you are looking for
        }
        text1.Text = title.InnerText;
        if (!pr.Equals("") && pr != "")
        {
            text2.Text = pr;
        }
        else
        {
            MessageBox.Show("null");
        }
    }

I utilizeif block to assess whether or whether theforeach returns nothing. Does anybody have a solution to this issue? Any assistance would be appreciated. I'm grateful.

1
1
5/23/2017 11:51:35 AM

Popular Answer

Try this:

p = doc.DocumentNode.SelectNodes(".//p");

in place of

p = doc.DocumentNode.SelectNodes("//p");

since p is neither a class or an id, but a sort of tag.

1
5/25/2016 11:37:45 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow