HTML Agility Pack get all anchors' href attributes on page

c# html-agility-pack

Question

To an HTML file, I'm attempting to add links that were taken out of it.CheckBoxList (cbl_items ).

It now works, however the item's name is presented as HtmlAgilityPack rather than the link. HtmlNode. I tried utilizingDocumentElement in place ofNode But it said it didn't exist or something close.

How can I change the display of HTMLAgilityPack.HtmlNode to the URL?

I have so far attempted the following:

HtmlWeb hw = new HtmlWeb();
HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
doc = hw.Load(tb_url.Text);
foreach (HtmlNode link in doc.DocumentNode.SelectNodes("//a[@href]"))
{
  cbl_items.Items.Add(link);
}
1
20
10/25/2017 12:33:51 PM

Accepted Answer

You're integrating theHtmlNode To the objectCheckBoxList not the price of thehref attribute. You are seeing theHtmlNode 's ToString() considering that it is the greatest that theCheckBoxList may perform to show that item.

instead, you may useGetAttributeValue(string attribute, string defaultValue) to obtain thehref value of the characteristic.

HtmlWeb hw = new HtmlWeb();
HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
doc = hw.Load(tb_url.Text);
foreach (HtmlNode link in doc.DocumentNode.SelectNodes("//a[@href]"))
{
    // Get the value of the HREF attribute
    string hrefValue = link.GetAttributeValue( "href", string.Empty );
    cbl_items.Items.Add(hrefValue);
}
24
9/5/2014 3:26:24 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